Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
178
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

98

Part II

HANDLING DATA

 

 

 

 

 

When you call the Abort() method of the Thread class, the method throws the

 

ThreadAbortException exception. In addition to the base class that is used to

 

handle threads, C# provides base classes to generate exceptions. The Thread-

 

AbortException is an exception of the ThreadAbortException class. C# provides

 

you with no mechanism to handle this exception. In other words, if you try to

 

abort the thread that is being executed inside the try block, the C# compiler first

 

executes any associated finally blocks before aborting the thread.

 

As you have seen, .NET provides you with a mechanism for safer killing of

 

threads compared with the earlier environments that killed the targeted thread

 

instantly. Having learned about creating and aborting threads, the next section

 

 

 

 

Y

 

will continue the discussion about working with threads.It will discuss the Join()

 

method that allows you to wait for a threadLto finish execution or to be killed by

 

the Abort() method.

F

 

Joining Threads

M

 

 

A

 

 

 

 

 

 

C# allows you to wait for a thread to terminate before the C# compiler proceeds

 

 

 

E

 

 

with the execution of the other thread. To do so, the Thread class contains a Join()

 

method, which takesTthe following syntax:

 

Thread1.Join();

The previous statement calls the Join() method of the Thread class to wait for Thread1 to terminate. If you do not know the time the thread takes to terminate, you can also specify the maximum time for which you want the C# compiler to wait before proceeding with the execution of the next thread. If the maximum time limit is not specified, the compiler waits for the thread to terminate on its own.

The Join() method is often used with the Abort() method. As explained earlier, when you call the Abort() method, the thread is not terminated instantly if it is in the middle of the try block. This implies that you need to wait for the finally statements to be executed before the thread terminates. However, you might not know the time the C# compiler takes to execute the finally block, and you are not ready to wait for a long period of time. Therefore, you can call the Abort() method followed by the Join() method to terminate the thread.

Team-Fly®

THREADS

Chapter 6

99

 

 

 

You have learned how to abort and join a thread. In some cases, you might only need to stop or suspend the execution of a thread for a specified time. The following section discusses suspending threads.

Suspending Threads

You have learned about aborting threads. When a thread is aborted, you cannot resume the execution of the thread. However, when you suspend a thread, you can resume its execution whenever required. To suspend the execution of a thread, you use the Suspend() method. The Suspend() method is another method of the Thread class and does not take any parameters. The syntax of the Suspend() method is:

Thread1.Suspend();

The Suspend() method does not kill the thread permanently. It just stops the execution of the thread until it is resumed. Therefore, when you need to restart the execution of a thread, you can call another method of the Thread class, the Resume() method. The Resume() method starts executing the thread from the point at which the execution was suspended. The syntax of the Resume() method is shown in the following code.

Thread1.Resume();

TIP

You can only resume the execution of a suspended thread.

Similar to the Abort() method, the Suspend() method does not instantly stop the execution of the targeted thread. It waits for the thread to reach a safe point before suspending it.

You can also call the Suspend() and Resume() methods from an executing thread. Therefore, a thread can call the Suspend() method to suspend itself or another thread.

For example, if thread1 suspends itself, another thread needs to call the Resume() method to restart its execution. However, if thread1 suspends another thread,such

100

Part II

HANDLING DATA

 

 

 

as thread2, the execution of thread2 is not resumed until thread1 calls the

Resume() method on thread2.

In addition to the Suspend() method, you can block the execution of a thread by calling the Sleep() method of the Thread class.

Making Threads Sleep

The Thread class contains another method, called the Sleep() method, to stop the execution of a thread for a specified time. You can specify the time for which you want to stop the execution of a thread by passing the time as a parameter to the Sleep() method. The time is specified in milliseconds. Consider the following example that puts the thread to sleep for 2 seconds.

Thread.Sleep(2000);

As you can see in the previous code, the Sleep() method is called by the class itself and not the instance of the class.

You may wonder how the Sleep() method is different from the Suspend() method. Both of these methods are used to stop the execution of a thread for some time. You have seen that the Suspend() method does not instantly stop the execution of the thread. It waits for the thread to reach a safe point before stopping its execution. However, if you need to block the execution of a thread immediately, you can do this by calling the Sleep() method instead of the Suspend() method.

Figure 6-1 shows the difference between the Sleep() and Suspend() methods.

FIGURE 6-1 Differences between the Suspend() and Sleep() methods

THREADS

Chapter 6

 

101

 

 

 

 

 

Until now, you have learned about various methods that can be used with threads. To understand more about these operations, here is a simple thread with operations performed on it.

using System;

using System.Threading; class Class1

{

public void Method1()

{

Console.WriteLine(“Method1 is the starting point of execution of the thread”);

}

public static void Main()

{

Class1 newClass = new Class1();

Thread Thread1 = new Thread(new ThreadStart(newClass.Method1));

Thread1.Name = “Sample Thread”;

Thread1.Start ();

Console.WriteLine (“The execution of Sample Thread has started.”);

Thread1.Abort();

}

}

The previous code imports the System and System.Threading namespaces in your program code. The code then creates a class with the name Class1 and declares a method Method1 in the class.This method is specified as the starting point of execution of a thread named Sample Thread, which is an instance of the Thread class. Next, an instance of the ThreadStart delegate is created that takes Method1 as the parameter. The instance of the Thread class is used to call the methods of the Thread class that perform operations on Sample Thread.

The output of the previous code is shown in Figure 6-2.