Like java, writing a multi-threaded application in C # is very simple. This chapter describes how to develop multi-threaded programs in C. In. net, threads are defined by the system. Threading namespace. Therefore, you must include this namespace.
Using system. Threading;
Start a thread
The thread class in the system. Threading namespace describes a thread object. By using a class object, you can create, delete, stop, and restore a thread. Create a new thread and start the thread through the new operation.
Thread = new thread (New threadstart (helloworld ));
Thread. Start ();
Note: Unlike Java programs, after creating a new thread and calling the START () method, it does not call the run () method, but passes the thread call program.
The following are the functions executed by the startup thread:
Protected void helloworld ()
{
String STR;
Console. Write ("helloworld ");
}
}
Killing a thread
The abort () method of the thread class can permanently kill a thread. Before killing a thread, you should determine whether the thread is in the active state.
If (thread. isalive)
{
Thread. Abort ();
}
Stop a thread
The thread. Sleep method can stop a thread in a fixed cycle class.
Thread. Sleep ();
Set thread priority
The threadpriority attribute in the Thread class is used to set the priority of a threadpriority. The thread priority levels include normal, abovenormal, belownormal, highest, and lowest.
Thread. Priority = threadpriority. Highest;
Suspends a thread
Calling the suspend () method of the thread class suspends a thread until it is invoked using the resume () method. Before a thread is suspended, determine whether the thread is in the active state.
If (thread. threadstate = threadstate. Running)
{
Thread. Suspend ();
}
Call a thread
The resume () method can be used to call up a suspended thread. Before suspending a thread, you should determine whether the thread is in the suspension period. If
The method does not work if the thread is not suspended.
If (thread. threadstate = threadstate. Suspended)
{
Thread. Resume ();
}
In many cases, you may want to execute a slightly different task in each thread. At the same time, you need to pass a parameter from the thread where one task is located to the thread where another task is located. There are several reasonable ways to accomplish this goal. The most direct approach is to create a task object that stores threads, special parameters, and worker Methods assigned by threadstart. The worker method can be used to read the provided parameters. Because it is a member of the task object, it is of course unique to the thread. By making a thread a public field, you can obtain the permissions to access all the members of the thread without writing additional encapsulation code. See program list B to read related examples of this technology.
Program list B
Using system;
Using system. Threading;
Namespace taskdemo ...{
Public class mytask ...{
Public thread m_thread;
String m_name;
Public mytask (string name )...{
M_name = Name;
M_thread = new thread (New threadstart (worker ));
}
Private void worker ()...{
Console. writeline ("hello ,");
Thread. Sleep (1500 );
Console. writeline (m_name );
}
}
Class taskdemo1 ...{
Static void main (string [] ARGs )...{
Mytask task1 = new mytask ("bill ");
Mytask task2 = new mytask ("Steve ");
Task1.m _ thread. Start ();
Task2.m _ thread. Start ();
}
}
}
Close the window end thread
Private void form1_closing (Object sender, system. componentmodel. canceleventargs E)
...{
If (my_thread.threadstate.tostring () = "suincluded ")
...{
My_thread.resume ();
My_thread.abort ();
My_thread.join ();
}
Else
...{
My_thread.abort ();
My_thread.join ();
}
}