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

94

Part II

HANDLING DATA

 

 

 

In this chapter, you will be introduced to threads. In addition, you will learn to create and work with threads. This chapter introduces you to the Thread class, which is a .NET base class.This class helps you to create and manipulate threads in applications. This chapter also discusses the states and priorities of threads. Finally, the chapter introduces you to the synchronization of variables across

threads.

Introduction to Threads

Threads are a well-known concept to C and C++ programmers. A thread is a basic unit of the execution of a program. In other words, a thread is the smallest unit to which the operating system allocates processor time. A thread decides the sequence of execution of a program and is very useful for executing complex applications or even multiple applications simultaneously. In addition, you can have a single application containing multiple threads. When the C# compiler executes a multithreaded application, several threads are executed simultaneously. This makes the execution of a complex application less time-consuming.

In a multithreaded application, you can execute multiple activities simultaneously. For example, consider a situation in which you execute a print command for printing 100 pages. Printing 100 pages takes a substantial amount of time. Therefore, you can have two threads working simultaneously on the system. One thread can be used for printing and the other thread can be used to perform any other activity, such as working in a Word document or a spreadsheet.

All the applications that you create involve one or more threads. However, there are some situations in which threads can be used very effectively. Following are examples of some of these situations.

As discussed earlier, you use threads to perform operations that can be time-consuming. In such cases, you can create two threads, a worker thread and a user thread. The worker thread performs time-consuming operations, and the user thread manages user interactions. For example, you can create a worker thread to print 100 pages while the user thread enables you to work in a Word document.

THREADS

Chapter 6

 

95

 

 

 

 

 

You can also use threads to transfer data over the network. For example, you need to transfer volumes of data from one branch office to another. In this case, you can create a thread to connect to the server in the other branch.

You also use threads when you need to execute an application that performs more than one operation. For example, when a data entry operator enters data into a database, this data should be updated automatically in the master database. In this case, you can have a worker thread and a user thread. The user thread accepts the input from the user while the worker thread updates the records in the master database.

You have seen that using threads in your application allows you to perform multiple activities simultaneously. However, extensive use of threads in a single application may even deteriorate the performance of your application. To understand this, let us look at the process of execution of threads. Executing threads requires the use of several operating system resources.These resources execute a thread for a very short period of time, known as the time slice of the thread. After executing the thread for this time slice, the Windows operating system chooses another thread to execute.This process of executing multiple threads for a given time slice is called preemptive multitasking. If you have multiple threads in a single application, the operating system spends time switching between various threads after a time slice, which may in turn reduce the performance of the application.

By now, you know that your application can have as many threads as required.The next section looks at creating threads for your application.

Creating Threads

A thread that you create is an instance of the Thread class. The Thread class is a class in the .NET Framework class library and is located in the System.Threading namespace. Therefore, to create an instance of the Thread class, you first need to import the System.Threading namespace. You can then create the object of the Thread class that represents a thread. You can continue to add threads to your application by simply creating multiple instances of the object of the Thread class.

To create a thread, you need to declare an instance of the Thread class and provide it with the details of the method with which the execution of the thread starts. To do so, you can use the public void delegate named ThreadStart() of the System.Threading namespace. You have learned about delegates in Chapter 1, “Overview of the .NET Framework,” in the section “Delegates.”

Update Records

96

Part II

HANDLING DATA

 

 

 

Consider the following example.

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));

}

}

Here, an instance of the Thread class, Thread1, is created. The ThreadStart() delegate specifies the name of the method, Method1, with which the execution of Thread1 starts. Method1 is a public void function defined in Class1. However, creating an instance of the Thread class does not make the thread functional. To start the thread, you need to call the Start() method. The following code shows the syntax for calling the Start() method.

Thread1.Start();

TIP

Because the threads that you define can be used across applications, it is advisable that you give a relevant name to your thread.This enables other programmers to reuse the functionality provided by your thread. To give a relevant name to your thread, you can change the value of the Name property of the thread.

The following code defines a worker thread for updating the records in the master database. It will give the thread a meaningful name, such as

Thread.

THREADS

Chapter 6

97

 

 

 

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

Thread1.Name = “Update Records Thread”;

The previous code snippet creates an instance of the Thread class with the name Thread1 and then assigns a name Update Records Thread to the thread.

In addition to the Name property that is used to give a meaningful name to a thread, you can use properties to know the status of the executing threads.These properties are defined in the Thread class of the System.Threading namespace.

IsAlive property. The IsAlive property is used to specify that the execution of a thread is complete or the thread is still working. The IsAlive property returns a Boolean value of true for the thread that is working and false for the thread that is not executing.

ThreadState property. The ThreadState property indicates the execution status of a thread. In other words, it returns a value specifying whether the execution of the thread has started or not. You will learn about the states of a thread later in this chapter.

Aborting Threads

You have learned how to create and execute a thread. However, sometimes you may need to stop a running thread. Consider the same old example in which you executed a print command for 100 pages. In this case, a thread is executed to print the required pages. If you need to print some other urgent page, you need to stop the previous print command or, in other words, abort the thread that is printing 100 pages. This section discusses aborting a running thread.

C# provides you with a base class, the Thread class, that you can use to perform several operations with threads. The Thread class contains several predefined methods that enable you to work with threads. To abort a thread, you use the Abort() method of the Thread class. The Abort() method has the following syntax:

Thread1.Abort();

Here, Thread1 is the instance of the Thread class. The Abort() method does not take any parameters. When you call the Abort() method, the C# compiler might not kill the thread instantaneously. To understand why the C# compiler takes time to kill the thread, you first need to understand how the Abort() method is executed.