Using system;
Using system. Threading;
Class threadtest
{
// Workthread Method
Public static void workerthreadmethod1 ()
{
// Obtain the currently executing thread object
Thread W1 = thread. currentthread;
W1.name = "WORKER 1 is working ";
For (INT I = 1; I <10; I ++)
{
Thread. Sleep (400 );
Console. writeline (w1.name );
If (I = 4)
{
Console. writeline ("I'm exhausted, don't want to do it ");
// Terminate the thread
W1.interrupt ();
}
}
}
Public static void main ()
{
// Used to encapsulate the delegate of a worker thread
Threadstart worker1 = new threadstart (workerthreadmethod1 );
Console. writeline ("[main function] start to create a working thread ");
// Create a new thread instance and use the worker1 delegate as the parameter. This indicates that this thread executes the methods encapsulated by the delegate worker1.
Thread T1 = new thread (worker1 );
// Start the thread
T1.Start ();
// Block the current main thread until thread t1 is terminated
T1.Join ();
Console. WriteLine ("[main function] Working thread ends ");
Console. ReadLine ();
}
}
The output result is:
[Main function] starts to create a working thread
WORKER 1 is working...
WORKER 1 is working...
WORKER 1 is working...
WORKER 1 is working...
I'm exhausted and don't want to do it anymore.
[Main function] The worker thread ends.
As you can see, the Thread. Interrupt () method can terminate it. So I don't want it to be terminated, but I just want it to be paused. What should I do? Can I only use Thread. Sleep? This is not easy to control, but it is time-consuming. So we will do this:
Using System;
Using System. Threading;
Class ThreadTest
{
// Workthread Method
Public static void WorkerThreadMethod1 ()
{
// Obtain the currently executing thread object
Thread w1 = Thread. CurrentThread;
W1.Name = "WORKER 1 is working ";
For (int I = 1; I <10; I ++)
{
Thread. Sleep (400 );
Console. writeline (w1.name );
}
}
Public static void main ()
{
// Used to encapsulate the delegate of a worker thread
Threadstart worker1 = new threadstart (workerthreadmethod1 );
Console. writeline ("[main function] start to create a working thread ");
// Create a new thread instance and use the worker1 delegate as the parameter. This indicates that this thread executes the methods encapsulated by the delegate worker1.
Thread t1 = new Thread (worker1 );
// Start the thread
T1.Start ();
Console. WriteLine ("I want to come first, stop it for me! ");
T1.Suspend ();
For (int I = 1; I <6; I ++)
{
Thread. Sleep (400 );
Console. WriteLine ("this movie looks really good, enjoy ing ");
}
Console. WriteLine ("Okay, I'm done, let's get started ");
T1.Resume ();
T1.Join ();
Console. WriteLine ("[main function] Working thread ends ");
Console. ReadLine ();
}
}
The output result is:
[Main function] starts to create a working thread
I want to come first. Stop it for me!
This movie looks really good and enjoys ing...
This movie looks really good and enjoys ing...
This movie looks really good and enjoys ing...
This movie looks really good and enjoys ing...
This movie looks really good and enjoys ing...
Okay, I'm done. Let's get started.
WORKER 1 is working...
WORKER 1 is working...
WORKER 1 is working...
WORKER 1 is working...
WORKER 1 is working...
WORKER 1 is working...
WORKER 1 is working...
WORKER 1 is working...
WORKER 1 is working...
[Main function] The worker thread ends.
We use the Thread. Suspend method to pause the specified Thread, and then we can use the Thread. Resume method to restore it.
Note the differences between the Sleep method and the Suspend method:
1. Suspend
The method has no parameters. The Thread terminated by the method can only be resumed by another Thread (same as the Thread. Resume method ). (Correspondingly, if the Sleep method is passed
Timeout. Infinite value, so it cannot be restored. It can be terminated only by calling Thread. Interrupt of the paused Thread by another Thread)
2. The Thread. Suspend method can be called on the current Thread or another Thread, while the Thread. Sleep method can only be called on the current Thread.
(So we remove the t1.Suspend () Statement and add it to WorkerThreadMethod1.
As follows:
// Obtain the currently executing thread object
Thread W1 = thread. currentthread;
W1.name = "WORKER 1 is working ";
W1.suspend ();
The results are the same as they used to be.
)
3. When a thread uses the Suspend method to Suspend another thread, the first thread is not locked. Return immediately after the call.
In addition, no matter how many times the Thread. Suspend method is called for a given Thread, a Thread. Resume method can be called once to Resume the Thread execution.
Collect others blog: http://www.cnblogs.com/wdxinren/archive/2004/12/09/74891.html