I believe that when we process a large amount of data in. NET, we will inevitably use threads.
We should know several methods of the thread:
Define four threads
ReceiveThread = new Thread(new ThreadStart(ReceiveFunc)); SaveThread = new Thread(new ThreadStart(SaveFunc)); SendThread = new Thread(new ThreadStart(SendFunc)); DataThread = new Thread(new ThreadStart(showDataToList));
Instantiation thread:
public Thread ReceiveThread { get; set; } public Thread SaveThread { get; set; } public Thread SendThread { get; set; } public Thread DataThread { get; set; }
Enable thread:
ReceiveThread. Start (); // Start the thread DataThread. Start (); SendThread. Start (); DataThread. Start ();
Pending thread:
Deleethread. Suspend (); // suspends the thread DataThread. Suspend (); SendThread. Suspend (); DataThread. Suspend ();
Recovery thread:
Deleethread. Resume (); // restoration thread DataThread. Resume (); SendThread. Resume (); DataThread. Resume ();
Termination thread:
Deleethread. Abort (); // terminate the thread DataThread. Abort (); SendThread. Abort (); DataThread. Abort ();
You try multiple times, suspend and recover, and you will find that the thread is dead;
How can this problem be solved?
I think of a stupid method. In the main thread loop, add a while loop and use a flag to control the thread sleep. When the thread needs to be paused, flag is set to ON, the thread will enter the while LOOP without stopping sleep. When the thread needs to be restored, the flag will be directly off and the thread will be restored. Moreover, the thread runs normally multiple times;
Instance code:
While (Flag_Receive | ReceiveThreadSleepFlag) {while (ReceiveThreadSleepFlag) {Thread. Sleep (1); // The Thread sleep waits for the continue;} // The main Thread}
The above Flag_Receive is the main thread running flag, and the ReceiveThreadSleepFlag is the thread sleep flag.
It provides a way of thinking, hoping that readers who have read this article will be able to make a good result.