0 Replies - 160 Views - Last Post: 01 January 2008 - 09:45 AM

#1 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Check if a thread is a background thread

Posted 01 January 2008 - 09:45 AM

Description: Call the method, it will return true/false based on the background status of the threadThis is a snippet I use to check if a thread (usually the current thread) is a background thread or not. You could also pass a Thread to the method and check that thread as well
//Namespace Reference
using System;
using System.Threading

/// 
/// method to determine if the current thread is a background thread
/// 
/// 
public bool IsBackgroundThread()
{
    //variable to hold our return value
    bool background = false;
    try
    {
        //get the current thread
        Thread current = Thread.CurrentThread;
        //find out if its a background thread
        background = current.IsBackground;
    }
    catch (ThreadStateException ex)
    {
        background = false;
        MessageBox.Show(ex.Message);
    }
    catch (Exception ex)
    {
        background = false;
        MessageBox.Show(ex.Message);
    }
    return background;
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1