Multi-thread processing in. NET

Source: Internet
Author: User

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.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.