22.1 Creating and Starting Threads

 < Day Day Up > 

You want to explicitly create and start a new thread to perform some processing task.


Technique

There are four things you need to do to explicitly create a thread:

  1. First, you need to decide on an entry point method. In a sense, the purpose of the thread is to execute this method. It can be either a static or instance method of a class, but it must have the signature void SomeMethod() ; that is, it cannot take any parameters nor can it return any value.

  2. Next, you need to instantiate a delegate that refers to this method. Microsoft defined a delegate for this purpose, System.Threading.ThreadStart .

  3. Now you can create the thread, represented by a System.Threading.Thread instance. The constructor for this class takes one parameter, a ThreadStart delegate, which indicates the method to be executed by the thread.

  4. Finally, you start the thread using the Thread.Start() method.

When you put all these steps together, the code looks like this. First, let us suppose that this is the method you want to be executed on a new thread:

 
 class WorkerClass {         public void DoSomeWork()         {                 // code to do the work here         } } 

You can get this method executed on a new thread using this code:

 
 WorkerClass worker = new WorkerClass(); ThreadStart workerThreadStart = new ThreadStart(worker.DoSomeWork); Thread workerThread = new Thread(workerThreadStart); workerThread.Start(); 

Comments

The advantage of starting a thread this way is that you get a reference to a managed object that encapsulates the new thread. With the preceding code, that reference is stored in the workerThread variable. It gives you a fine degree of control over the new thread; if you store this reference, then any other running thread in your application can use the reference to manage the worker thread, for example, by pausing, resuming, or aborting it or by changing its priority.

Provided the new thread is not aborted, it exits normally when its entry method (with the preceding code, the DoSomeWork() method) exits. This exit can happen because the flow of execution has reached a return statement, or it can be because some unhandled exception has been thrown.

If another thread wants to know whether the worker thread has exited yet, it can use the Thread.ThreadState property:

 
 if (workerThread.ThreadState == ThreadState.Stopped)         Console.WriteLine("The worker thread has exited!"); 

The ThreadState property returns a ThreadState enumeration value: Possible return values appear in Table 22.1.

Table 22.1. ThreadState Enumeration Values

Value

Meaning

Unstarted

Thread hasn't started yet.

Running

Thread is currently executing.

WaitSleepJoin

Thread is asleep.

SuspendRequested

Thread is about to be suspended .

Suspended

Thread is suspended.

AbortRequested

Thread is in the process of being aborted.

Stopped

Thread has stopped.

The strict requirement that the thread's entry method cannot take any parameters means that you can't use parameters to pass any data to the thread. If you need to supply some initial data to the worker thread, the easiest way is to initialize some member variables of the object containing the thread's entry method before the thread starts. For example, suppose that the worker thread needs to know the name of some file that it is going to process. Modifying our earlier code gives the following, which first includes the WorkerClass definition:

 
 class WorkerClass {         public WorkerClass(string fileName)         {                 this.fileName = fileName;         }         string fileName;         public void DoSomeWork()         {                 // code to do the work here.         } } 

Next is the code to create and start the thread:

 
 WorkerClass worker = new WorkerClass(@"C:\ProcessMe.txt"); ThreadStart workerThreadStart = new ThreadStart(worker.DoSomeWork); Thread workerThread = new Thread(workerThreadStart); workerThread.Start(); 

Note that once the worker thread starts, you should take care to synchronize access to any of its member fields if those fields can be accessed by other threads. We show the techniques for doing so later in this chapter.

 < Day Day Up > 


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net