Running The thread has been started, it is not blocked, and there is no pending ThreadAbortException.
StopRequested The thread is being requested to stop. This is for internal use only.
SuspendRequested The thread is being requested to suspend.
Background The thread is being executed as a background thread, as opposed to a foreground thread. This state is controlled by setting the Thread.IsBackground property.
Unstarted The Thread.Start method has not been invoked on the thread.
Stopped The thread has stopped.
WaitSleepJoin The thread is blocked. This could be the result of calling Thread.Sleep or Thread.Join, of requesting a lock — for example, by calling Monitor.Enter or Monitor.Wait — or of waiting on a thread synchronization object such as ManualResetEvent
AbortRequested The Thread.Abort method has been invoked on the thread, but the thread has not yet received the pending System.Threading.ThreadAbortException that will attempt to terminate it.
Aborted The thread state includes AbortRequested and the thread is now dead, but its state has not yet changed to Stopped.
Collection and sharing of, interview questions and answers asked in various interviews, faqs and articles.....
C# Sample code: starts a worker thread, and a Timer
using System;
using System.Threading;
namespace CallBacks
{
class Program
{
private string message;
private static Timer timer;
private static bool complete;
static void Main(string[] args)
{
Program p = new Program();
Thread workerThread = new Thread(p.DoSomeWork);
workerThread.Start();
//create timer with callback
TimerCallback timerCallBack =
new TimerCallback(p.GetState);
timer = new Timer(timerCallBack, null,
TimeSpan.Zero, TimeSpan.FromSeconds(2));
//wait for worker to complete
do
{
//simply wait, do nothing
} while (!complete);
Console.WriteLine("exiting main thread");
Console.ReadLine();
}
public void GetState(Object state)
{
//not done so return
if (message == string.Empty) return;
Console.WriteLine("Worker is {0}", message);
//is other thread completed yet, if so signal main
//thread to stop waiting
if (message == "Completed")
{
timer.Dispose();
complete = true;
}
}
public void DoSomeWork()
{
message = "processing";
//simulate doing some work
Thread.Sleep(3000);
message = "Completed";
}
}
}
using System.Threading;
namespace CallBacks
{
class Program
{
private string message;
private static Timer timer;
private static bool complete;
static void Main(string[] args)
{
Program p = new Program();
Thread workerThread = new Thread(p.DoSomeWork);
workerThread.Start();
//create timer with callback
TimerCallback timerCallBack =
new TimerCallback(p.GetState);
timer = new Timer(timerCallBack, null,
TimeSpan.Zero, TimeSpan.FromSeconds(2));
//wait for worker to complete
do
{
//simply wait, do nothing
} while (!complete);
Console.WriteLine("exiting main thread");
Console.ReadLine();
}
public void GetState(Object state)
{
//not done so return
if (message == string.Empty) return;
Console.WriteLine("Worker is {0}", message);
//is other thread completed yet, if so signal main
//thread to stop waiting
if (message == "Completed")
{
timer.Dispose();
complete = true;
}
}
public void DoSomeWork()
{
message = "processing";
//simulate doing some work
Thread.Sleep(3000);
message = "Completed";
}
}
}
C# small program with a main thread and 2 worker threads
using System;
using System.Threading;
namespace StartingThreads
{
class Program
{
static void Main(string[] args)
{
//no parameters
Thread workerThread = new Thread(StartThread);
Console.WriteLine("Main Thread Id {0}",
Thread.CurrentThread.ManagedThreadId.ToString());
workerThread.Start();
//using parameter
Thread workerThread2 = new Thread(ParameterizedStartThread);
// the answer to life the universe and everything, 42 obviously
workerThread2.Start(42);
Console.ReadLine();
}
public static void StartThread()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Thread value {0} running on Thread Id {1}",
i.ToString(),
Thread.CurrentThread.ManagedThreadId.ToString());
}
}
public static void ParameterizedStartThread(object value)
{
Console.WriteLine("Thread passed value {0} running on Thread Id {1}",
value.ToString(),
Thread.CurrentThread.ManagedThreadId.ToString());
}
}
}
using System.Threading;
namespace StartingThreads
{
class Program
{
static void Main(string[] args)
{
//no parameters
Thread workerThread = new Thread(StartThread);
Console.WriteLine("Main Thread Id {0}",
Thread.CurrentThread.ManagedThreadId.ToString());
workerThread.Start();
//using parameter
Thread workerThread2 = new Thread(ParameterizedStartThread);
// the answer to life the universe and everything, 42 obviously
workerThread2.Start(42);
Console.ReadLine();
}
public static void StartThread()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Thread value {0} running on Thread Id {1}",
i.ToString(),
Thread.CurrentThread.ManagedThreadId.ToString());
}
}
public static void ParameterizedStartThread(object value)
{
Console.WriteLine("Thread passed value {0} running on Thread Id {1}",
value.ToString(),
Thread.CurrentThread.ManagedThreadId.ToString());
}
}
}
Starting Threads [Code Sample]
Starting new Threads is pretty easy, we just need to use one of the Thread constructors, such as
Thread(ThreadStart)
Thread(ParameterizedThreadStart)
There are others, but these are the most common ways to start threads. Lets look at an example of each of these
Code With No Parameters
Thread workerThread = new Thread(StartThread);
Console.WriteLine("Main Thread Id {0}",
Thread.CurrentThread.ManagedThreadId.ToString());
workerThread.Start();
....
....
public static void StartThread()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Thread value {0} running on Thread Id {1}",
i.ToString(),
Thread.CurrentThread.ManagedThreadId.ToString());
}
}
Code With Single Parameter
//using parameter
Thread workerThread2 = new Thread(ParameterizedStartThread);
// the answer to life the universe and everything, 42 obviously
workerThread2.Start(42);
Console.ReadLine();
....
....
public static void ParameterizedStartThread(object value)
{
Console.WriteLine("Thread passed value {0} running on Thread Id {1}",
value.ToString(),
Thread.CurrentThread.ManagedThreadId.ToString());
}
Thread(ThreadStart)
Thread(ParameterizedThreadStart)
There are others, but these are the most common ways to start threads. Lets look at an example of each of these
Code With No Parameters
Thread workerThread = new Thread(StartThread);
Console.WriteLine("Main Thread Id {0}",
Thread.CurrentThread.ManagedThreadId.ToString());
workerThread.Start();
....
....
public static void StartThread()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Thread value {0} running on Thread Id {1}",
i.ToString(),
Thread.CurrentThread.ManagedThreadId.ToString());
}
}
Code With Single Parameter
//using parameter
Thread workerThread2 = new Thread(ParameterizedStartThread);
// the answer to life the universe and everything, 42 obviously
workerThread2.Start(42);
Console.ReadLine();
....
....
public static void ParameterizedStartThread(object value)
{
Console.WriteLine("Thread passed value {0} running on Thread Id {1}",
value.ToString(),
Thread.CurrentThread.ManagedThreadId.ToString());
}
how to execute code in a specific AppDomain
using System;
using System.Threading;
namespace LoadNewAppDomain
{
class Program
{
static void Main(string[] args)
{
AppDomain domainA = AppDomain.CreateDomain("MyDomainA");
AppDomain domainB = AppDomain.CreateDomain("MyDomainB");
domainA.SetData("DomainKey", "Domain A value");
domainB.SetData("DomainKey", "Domain B value");
OutputCall();
domainA.DoCallBack(OutputCall); //CrossAppDomainDelegate call
domainB.DoCallBack(OutputCall); //CrossAppDomainDelegate call
Console.ReadLine();
}
public static void OutputCall()
{
AppDomain domain = AppDomain.CurrentDomain;
Console.WriteLine("the value {0} was found in {1}, running on thread Id {2}",
domain.GetData("DomainKey"),domain.FriendlyName,
Thread.CurrentThread.ManagedThreadId.ToString());
}
}
}
using System.Threading;
namespace LoadNewAppDomain
{
class Program
{
static void Main(string[] args)
{
AppDomain domainA = AppDomain.CreateDomain("MyDomainA");
AppDomain domainB = AppDomain.CreateDomain("MyDomainB");
domainA.SetData("DomainKey", "Domain A value");
domainB.SetData("DomainKey", "Domain B value");
OutputCall();
domainA.DoCallBack(OutputCall); //CrossAppDomainDelegate call
domainB.DoCallBack(OutputCall); //CrossAppDomainDelegate call
Console.ReadLine();
}
public static void OutputCall()
{
AppDomain domain = AppDomain.CurrentDomain;
Console.WriteLine("the value {0} was found in {1}, running on thread Id {2}",
domain.GetData("DomainKey"),domain.FriendlyName,
Thread.CurrentThread.ManagedThreadId.ToString());
}
}
}
Subscribe to:
Posts (Atom)