Пользовательский элемент управления. Создание клиентского приложения. Развитие серверного проекта, страница 14

When to use Multithreading

¨  There are complex computations, which are going to take much time

¨  Some of the operations can be performed parallel

¨  The CPU has to wait for a response from a remote computer or server

¨  Long I/O operations

¨  The application has to wait for a user input (Bosh, Peter; 2002)

When not to use Multithreading

¨  Because it is just cool, you have to have at least one of the above conditions in order to employ multithreading in your application

¨  You must have proven that a single threaded application is unacceptably slow, and using multiple threads will improve significantly the performance (Bosh, Peter; 2002)

Advantages of Multithreading

¨  Better responsiveness to the user – if there are operations which can take long time to be done, these operations can be put in a separate thread, and the application will preserve its responsiveness to the user input

¨  Faster application – the work will be done by more objects (Aitken, Peter; 2002)

Disadvantages of Multithreading

¨  Creating overhead for the processor – each time the CPU finishes with a thread it should write in the memory the point it has reached, because next time when the processor starts that thread again it should know where it has finished and where to start from

¨  The code become more complex – using thread into the code makes the code difficult read and debug

¨  Sharing the resources among the threads can lead to deadlocks or other unexpected problems (Aitken, Peter; 2002)

Methods for creating threads in .NET

There are two methods for creating threads in a .NET application:

¨  Thread pool

¨  Thread class

Using the Thread class to create and manage threads

To declare a thread we use the following code:

Thread myThread = new Thread(myThreadStart);

Here we pass to the thread constrictor as a parameter the entry point where the thread is going to start to work. That is the method the thread is going to execute. Because we pass a method we should do this through a delegate. There is a predefined delegate in the System.Threading class and it is the following:

public delegate void ThreadStart();

The parameter passed to the constructor should be of this type too.

ThreadStart myThreadStart = new ThreadStart(myThreadClass.hello);

Here I have a class myThreadClass where I have a method called “hello”. This is going to be the start point for myThreadStart.

But only declaring the delegate and the thread is not going to start the thread we use the following code to start the thread:

myThread.Start();

Passing a parameter to a thread: (Bosh, Peter; 2002)

Sometimes may be you will need to create two or more threads with the same entry point but with different tasks. That means you will have to pass to the methods different parameters. One easy and direct way of doing this is to create an object, which holds the thread, a static field, which will hold the parameter, and the method to be executed.

public class Multiply // This is my class:

{

public Thread myThread; //This is the thread I an going to start:

string name;   // These two are the parameters I want to pass:

int counter;

// In the constructor of the class I instantiate the parameters to the passed values:

public Multiply (string pname, int pcounter)

{

name = pname;

counter = pcounter;

// and start the thread:

myThread = new Thread (new ThreadStart(start));

}

// This is the method the thread is going to execute:

private void Start()

{

  Console.WriteLine("Now thread " + name + " has started");

for (int i=1; i<=8*counter; i++)

{

     if (i%counter==0)

Console.WriteLine(name + ": count has reached " + i);

}