Source: C # Multi-threading, control thread count improves cycle output efficiency
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 Ten 0 Ten Ten 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.
intThreadcounttmp =0;//Task thread Dispatch number Private voidBtncreate_click (Objectsender, EventArgs e) { intSeed = Convert.ToInt32 (createcount.value)%Ten==0? Convert.ToInt32 (Createcount.value)/Ten: Convert.ToInt32 (Createcount.value)/Ten+1; for(inti =0; i < seed; i++) {Thread threadtmp=NewThread (NewParameterizedthreadstart (tempout)); Threadtmp.start (i); Threadcounttmp++; Application.doevents ();//Response window Status while(true) {if(Threadcounttmp <Ten) Break; }//Push-pull window control multithreading threads number }} //Post -segmented data is published to other threads Public voidTempout (Objecto) {intTmp=convert.toint32 (o) *Ten; inti =tmp; for(; i < (tmp+Ten<=createcount.value?tmp+Ten: Createcount.value); i++) {thread thread=NewThread (NewParameterizedthreadstart (resultout)); Thread. Start (i); ThreadCount++; while(true) {if(ThreadCount <Ten) Break; }//Push-pull window control multithreading threads number} 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 voidTexttmp (ObjectO);//declaring a delegate intThreadCount =0;//Task Thread//delegate Functions Public voidResultout (Objecto) {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=Newtexttmp (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 Span style= "color: #000000;" > Leng) { string result = for (int i = 0 ; I < Leng; I++ + =
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