How to generate a thread?
Create an instance of the system. Threading. thread object and pass the threadstart example to be executed in the new thread to it. For example:
Class mythread
{
Public mythread (string initdata)
{
M_data = initdata;
M_thread = new thread (New threadstart (threadmain ));
M_thread.start ();
}
// Threadmain () is executed on the new thread.
Private void threadmain ()
{
Console. writeline (m_data );
}
Public void waituntilfinished ()
{
M_thread.join ();
}
Private thread m_thread;
Private string m_data;
}
Here, creating an instance of mythread is enough to generate a thread and execute the mythread. threadmain () method:
Mythread T = new mythread ("Hello, world .");
T. waituntilfinished ();
--------------------------------------------------------------------------------
How to stop a thread?
There are several methods. First, you can use your communication mechanism to tell the threadstart method to end. In addition, the thread class has built-in support for command thread stop. The two basic methods are thread. Interrupt () and thread. Abort (). The former throws a threadinterruptedexception and then enters the waitjoinsleep state. In other words, thread. Interrupt is a courtesy method that requests the thread to stop itself when it no longer performs any useful work. Correspondingly, thread. Abort () throws a threadabortexception regardless of what the thread is doing. Moreover, threadabortexception cannot be captured as usual (even if the end method of threadstart will be executed ). Thread. Abort () is an uncommon method.
--------------------------------------------------------------------------------
How to Use the thread pool?
Class CAPP
{
Static void main ()
{
String S = "Hello, world ";
Threadpool. queueuserworkitem (New waitcallback (dowork), S );
Thread. Sleep (1000); // give time for work item to be executed
}
// Dowork is executed on a thread from the thread pool.
Static void dowork (object state)
{
Console. writeline (State );
}
}
--------------------------------------------------------------------------------
How can I know when my thread pool work project will be completed?
There is no way to ask such information about the thread pool. You must place code in the waitcallback method to send a signal to indicate that it has been completed. Events here are also very useful.
11.5.6 how to prevent concurrent access to data?
Each object has a concurrent lock associated with it (the criticized part ). The system. Threading. Monitor. Enter/exit method is used to obtain and release locks. For example, the following class instance only allows one thread to enter the method F () at the same time ():
Class C
{
Public void F ()
{
Try
{
Monitor. Enter (this );
...
}
Finally
{
Monitor. Exit (this );
}
}
}
C # The keyword 'lock' provides a simple form of the above Code:
Class C
{
Public void F ()
{
Lock (this)
{
...
}
}
}
Note that calling monitor. Enter (myobject) does not mean that all access to myobject is serialized. It means that the synchronization lock associated with the myobject is requested, and no other thread can request the lock before the monitor. Exit (o) is called. In other words, the following class is functionally equivalent to the class given above:
Class C
{
Public void F ()
{
Lock (m_object)
{
...
}
}
Private m_object = new object ();
}
Web development technology knowledge base: http://cn-web.com/cnweb/0/190/article/