C # multi-threaded auxiliary class multi-task implementation

Source: Internet
Author: User

Summary
A large data volume operation is required in the recent project, which takes a long time, so multithreading is required to improveProgramExecution speed.
Therefore, I wrote a multi-threaded helper class to set the number of multiple threads, and then I could reuse these threads to execute the assigned tasks.
It can be said that it is a simple multi-thread application, there is no advanced technology, I hope the garden's Daniel will give more instructions :)

Implementation Process
1) First, implement a multi-threaded helper class,CodeAs follows:

Code
Public   Class Threadmulti
{
Public   Delegate   Void Delegatecomplete ();
Public   Delegate   Void Delegatework ( Int Taskindex, Int Threadindex );

PublicDelegatecomplete completeevent;
PublicDelegatework workmethod;

private manualresetevent [] _ resets;
private int _ taskcount = 0 ;
private int _ threadcount = 5 ;

Public threadmulti ( int taskcount)
{< br> _ taskcount = taskcount;
}

PublicThreadmulti (IntTaskcount,IntThreadcount)
{
_ Taskcount=Taskcount;
_ Threadcount=Threadcount;
}

Public   Void Start ()
{
If (_ Taskcount < _ Threadcount)
{
// The number of tasks is smaller than the number of threads
_ Resets =   New Manualresetevent [_ taskcount];
For ( Int J =   0 ; J < _ Taskcount; j ++ )
{
_ Resets [J] =   New Manualresetevent ( False );
Threadpool. queueuserworkitem ( New Waitcallback (work ), New   Object [] {J, j });
}
}
Else
{
_ Resets =   New Manualresetevent [_ threadcount];
// If the number of tasks is greater than the number of threads, start the tasks with the number of threads first.
For ( Int I =   0 ; I < _ Threadcount; I ++ )
{
_ Resets [I] =   New Manualresetevent ( False );
Threadpool. queueuserworkitem ( New Waitcallback (work ), New   Object [] {I, I });
}
// After a task is completed, use the completed thread to execute the next task.
Int Receivereset =   0 ;
Receivereset = Manualresetevent. waitany (_ resets );
For ( Int L = _ Threadcount; L < _ Taskcount; L ++ )
{
_ Resets [receivereset]. Reset ();
Threadpool. queueuserworkitem ( New Waitcallback (work ), New   Object [] {L, receivereset });
Receivereset = Manualresetevent. waitany (_ resets );
}
}

Manualresetevent. waitall (_ resets );
If(Completeevent! = Null)
{
Completeevent ();
}
}

Public   Void Work ( Object Arg)
{
Int Taskindex =   Int . Parse ((( Object []) Arg )[ 0 ]. Tostring ());
Int Resetindex =   Int . Parse ((( Object []) Arg )[ 1 ]. Tostring ());
If (Workmethod ! =   Null )
{
Workmethod (taskindex +   1 , Resetindex + 1 );
}
_ Resets [resetindex]. Set ();
}
}

The threadmulti class can reuse multiple threads and execute the specified workmethod based on the number of incoming tasks and the number of threads.
The completeevent event can be triggered after the task is completed.
The key in the class is the manualresetevent class application. When a task is completed, it notifies the program which thread has completed the execution, and then schedules another task to start execution.

2) the application of helper classes should be quite simple.

// Instantiate multi-thread helper class and start
Threadmulti thread =   New Threadmulti (workcount );

thread. workmethod = New threadmulti. delegatework (dowork); // function for executing a task
thread. completeevent = New threadmulti. delegatecomplete (workcomplete); // all tasks completed
thread. start ();

You only need to specify the number of tasks, the function for executing the task, and the event after the task is completed. After the start method is called, dowork calls and transmits the index of the task and the index of the execution thread in the thread.

Public   Void Dowork ( Int Index, Int Threadindex)
{
}

We can operate the corresponding data based on the imported index. For example, if we want to import a large data table to the database, we can divide the entire table into several pages according to the paging function, it is equivalent to a task in our program. we load the data on the specified page in the database according to pageindex, and then sort the data into the specified table.
Of course, the execution progress information can be displayed in dowork.

Example


Download
/Files/zrx401558287/threadmultipro.rar
I have listened to your comments and made a modified version, which is indeed much more professional than before. I especially appreciate the valuable comments of Lao Yu and have gained a long experience.
The following is a new version. I hope you will give more suggestions.
/Files/zrx401558287/threadmultipronew.rar
The stop function is completed, and the start method is simplified according to hawker's suggestions. The following is an updated program. Let's take a look at it :)
/Files/zrx401558287/threadmultipro09081418.rar

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.