C # Multi-threading, control thread count improves cycle output efficiency

Source: Internet
Author: User

C # Multithreading and controlling the number of threads for the For loop output efficiency.

Although the output is irregular, the efficiency is obviously improved.

Ideas:

If you want to delete 1000 data, use a For loop only, then one output. So, 1000 pieces of data are divided into seed segments and 10 pieces of data per segment.

int seed = Convert.ToInt32 (createcount.value)% 10 = = 0? Convert.ToInt32 (Createcount.value)/10:convert.toint32 (Createcount.value)/10 + 1;

Note: The value of Createcount.value is the number of specific output data

Here the data is allocated to seed threads to process, and each thread only outputs 10 data.

        int threadcounttmp = 0;//Task thread is assigned the number private void Btncreate_click (object sender, EventArgs e) { int seed = Convert.ToInt32 (createcount.value)% 10 = = 0?            Convert.ToInt32 (Createcount.value)/10:convert.toint32 (Createcount.value)/10 + 1; for (int i = 0; i < seed; i++) {Thread threadtmp = new Thread (New Parameterizedthreadstart (T                Empout));                Threadtmp.start (i);                threadcounttmp++; Application.doevents ();//Response window state while (true) {if (Threadcounttmp < ten) break;} Push-pull window control multi-threaded Threads 10}}//Segment data post to other threads public void Tempout (object o) {int Tmp=co Nvert.            ToInt32 (o) *10;            int i = tmp; for (; i < (tmp+10<=createcount.value?tmp+10:createcount.value); i++) {Thread thread = NE                W Thread (New Parameterizedthreadstart (resultout)); Thread.                Start (i); Threadcount++; while (true) {if (ThreadCount < ten) break;}        Push-and-pull window control multithreading threads number of ten} threadcounttmp--; }

After the segmentation, then the segmented data is allocated to other threads to handle, so that the multi-threading can work at the same time, because of the control operation, so the use of multithreading to rely on the delegation to implement multi-threaded control of the control. So the output of the last step is as follows:

        delegate void Texttmp (object o);//Declaration delegate        int threadcount = 0;//task thread        //delegate function public        void Resultout (Object o)        {            if (!txtresult.invokerequired)            {                txtresult.text = "\ n" + F_groundcode. Text + "," + F_ticketno. Text + DateTime.Now.ToLongDateString (). Replace ("-", "") + Getzero (6-o.tostring (). Length) + o.tostring () + "," + DateTime.Now.ToLongDateString (). Replace ("-", "") + DateTime.Now.ToLongTimeString (). Replace (":", "") + Txtresult.text;            }            else            {                texttmp Tmpdel = new Texttmp (resultout);                This. Invoke (Tmpdel,o);            }            threadcount--;        }

Because my data has to guarantee the number of bits, so I want to do 0 simple processing. For example I want to output

000000

000001

000002

000003

........

As you can see from the code above, I use for to increment. So it's an integral type, and the previous 0 is changing the number of values as the number changes.

        How many 0        private string getzero (int leng)        {            string result = "            in front of the number to handle); for (int i = 0; i < Leng; i++)            {                result + = "0";            }            return result;        }

All right. A simple multithreading process. I hope everyone can learn. Welcome everyone to guide ~ ~

C # Multi-threading, control thread count improves cycle output efficiency

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.