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

Console.WriteLine("Thread " + name + " has finished");



(This function is from: Professional C# 2nd Edition; Wrox Press Ltd, May 2002). I call the class holding the thread through the following test class:

public class TestClass


static int counter;

static void Main()


counter = 1000000;

// Here I create an instance of multiply class, and pass to the constructor two unique parameters:

Multiply m1 = new Multiply("First", counter);


// Here I create another instance of multiply class and pass two other parameters:

Multiply m2 = new Multiply("Second", counter*2);





Because the system will provide both threads with different memory space to be done, the two parameters will have their unique values even if the threads are going to be executed simultaneously.

Thread priorities

You can assign different priorities to the threads you create in your application. That means you can tell the system which thread can be done first, which thread can interrupt others and which thread cannot be interrupted. The possible values are:

¨  Highest

¨  AboveNormal

¨  Normal

¨  BelowNormal

¨  Lowest

Each thread with higher priority can interrupt a thread with a lower priority level. The threads with Highest priority will be executed first and they cannot be interrupted. You can assign a priority level to the thread using the following code:

MyThread.Priority = ThreadPriority.Highest;

Stop a thread

Once a thread is start it can be stopped for a given period of time, resumed after that or even killed. You can make a thread to “sleep” for a given interval through the following:


Which means that the current thread will be stopped for 1000 milliseconds and resumed after that by the system. This is a static method of the thread class and cannot be used with a thread instance you have created. You can stop a thread instance by the Suspend() method:


This is thread will stay dormant until you call it back to action. You can resume that thread with the Resume() method:


The thread will be resumed at the point it has been suspended. You can also directly kill a thread with the Abort() method:


This is going to stop the thread and the system will destroy all the data related to that thread. Suspending and aborting a thread does not take place immediately. The system can allow the thread to do some more actions before being suspended or killed, in order to stop the thread when it is safe for the running application.

The Abort() method actually throws an exception ThreadAbortException in the affected thread. ThreadAbortException is a special exception class that cannot be handled. That means if the thread is in a try…catch block, it will pass the catch block and execute what is I the finally block. This is done in order to be ensured some cleaning process before shutting down the thread.

Wait for another thread

You can make the current thread to wait for another thread to be executed through the Join() method:


This is will make the current thread to stop execution while myThread is done. This method allows overloading, you can specify how much time the current thread to wait and after that to resume the execution. If there is no time specified, the calling thread will wait until the called thread is done.


This will make the calling thread to wait for 10 seconds and resume execution after that. We can check it myThread is still alive after the calling thread resumes:


IsAlive is a Boolean method, which returns true or false.