Home > Net >  Winform c # multithreaded process instance can download the DEMO
Winform c # multithreaded process instance can download the DEMO

Time:10-12

When development program in c #, we often use the multithreading, the processing of implement multitasking, commonly used method is a new multiple threads, processes,

Today I share a with the method of the thread pool for instance, to a friend in need to make a reference,

Example: Winform

 using System; 
using System.Collections.Generic;
Using System.Com ponentModel;
Using System. The Data;
Using System. Drawing;
Using System. The Text;
Using System. Windows. Forms;
Using System. The Threading;

The namespace MultiThreading
{
///& lt; Summary>
///this example: rapid development platform for the SDP software used in the real methods
///& lt;/summary>
Public partial class Form1: Form
{
///& lt; Summary>
///private: thread synchronization signal
///& lt;/summary>
Private ManualResetEvent cmdWaiter;

///& lt; Summary>
///update the progress bar
///& lt;/summary>
Private delegate void updateBar ();

///& lt; Summary>
///the end tip entrust
///& lt;/summary>
Private delegate void showEnd ();

///& lt; Summary>
///task queue
///note: this task queue requires the user to automatically to define
///instance of the string is used to deal with the
///& lt;/summary>
Private List TaskList=new List (a);

///& lt; Summary>
///constructor
///& lt;/summary>
Public _click ()
{
InitializeComponent();
}

///& lt; Summary>
///page load initialization
///& lt;/summary>
///& lt; Param name="sender" & gt; & lt;/param>
///& lt; Param name="e" & gt; & lt;/param>
Private void Form1_Load (object sender, EventArgs e)
{

//block the current thread
CmdWaiter=new ManualResetEvent (false);

//start the thread pool
ThreadPool. QueueUserWorkItem (new WaitCallback (enclosing On_ThreadEvent));

}

///& lt; Summary>

///thread to handle affairs///& lt;/summary>
///& lt; Param name="obj" & gt; & lt;/param>
Private void On_ThreadEvent (object obj)
{
While (true)
{
Try
{
//block the current thread, waiting for a command to remove
This. CmdWaiter. WaitOne ();

//implementation of we need to deal with affairs
For (int k=0; K & lt; TaskList. Count; K++)
{
Run_MyBusiness (taskList [k]);

//rest specified milliseconds
Thread.sleep (50);
}


//remove queue data
This. The taskList. The Clear ();

Enclosing On_EndLog ();
This. CmdWaiter. Reset ();
}
The catch (Exception e)
{
String the strError=e.M essage. ToString ();
This. The taskList. The Clear ();
This. CmdWaiter. Reset ();
}
}
}

///& lt; Summary>
///perform our own business
///& lt;/summary>
///& lt; Param name="STR" & gt; & lt;/param>
Private void Run_MyBusiness (string STR)
{
//update the
UpdateBar updateDelegate=new updateBar (On_Update);
Enclosing the Invoke (updateDelegate);
}

///& lt; Summary>
///start button
///& lt;/summary>
///& lt; Param name="sender" & gt; & lt;/param>
///& lt; Param name="e" & gt; & lt;/param>
Private void btn_Start_Click (object sender, EventArgs e)
{

Enclosing taskList=new List (a);
For (int I=1; i <1001; I++)
{
This. The taskList. Add (i.T oString ());
}


This. Txt_log. Text=":" + DateTime. Now. The ToString (" MM - dd yyyy - HH: MM: ss ") + Environment. The NewLine;


This. Pg_Bar. Value=https://bbs.csdn.net/topics/0;
This. Pg_Bar. Maximum=this. The taskList. Count;

//unblocked
This. CmdWaiter. The Set ();
}

///& lt; Summary>
///update the progress bar
///& lt;/summary>
Private void On_Update ()
{
Enclosing pg_Bar. Value=https://bbs.csdn.net/topics/this.pg_Bar.Value + 1;
}


Private void On_EndLog ()
{
ShowEnd endDeg=new showEnd (On_UpdateTextBox);
Enclosing the Invoke (endDeg);
}

Private void On_UpdateTextBox ()
{
End of the string runStr=":" + DateTime. Now. The ToString (" MM - dd yyyy - HH: MM: ss ");
This. Txt_log. Text=this. Txt_log. Text + runStr;
}
}
}


Running effect:


Download address: http://www.sdpsoft.com/demofiles/MultiThreading.rar

CodePudding user response:

Thanks for sharing the

CodePudding user response:

Use the
//block the current thread, waiting for a command to remove 
This. CmdWaiter. WaitOne ();


Effect is not good, establishment of synchronous waiting

  • Related