Multi-threaded ERP/MIS Development Model and Application (Open Source Code)

Source: Internet
Author: User

I have been engaged in ERP/MIS development. To sum up, I often use two multi-threaded application modes in ERP/MIS development.

 

Let's take an example to help you remember the impression of multithreading. Copyfilesproc is a method for copying files. It is called using multiple threads:

Thread simplethread = new thread (copyfilesproc );

Simplethread. Name = "copyfiles ";

Simplethread. Start ();

Start the call. In vs2010, a thread debugging window is added to view the thread of the current process. In my understanding, I always remember to name your thread.

 

1. For scenarios that require interaction with the interface, you need to implement the report progress and apply the backgroundworker component.

On the monthly interface, When you click the process button, a progress bar is displayed, showing the processing progress.
For the use of the backgroundworker control, refer to the msdn knowledge base. I will describe the progress report function here.

The report progress function is implemented in two cases. One is that the processing task (job) is not implemented using an interface, and the current progress variable exists in the form. The source code is as follows:

Private void bgworker_dowork (Object sender, doworkeventargs E)
{
Bgworker. reportprogress (10, filename );
}

Directly in the dowork event, call the reportprogress method. If possible, you can also input variable values.

Private void backgroundworker_progresschanged (Object sender, progresschangedeventargs E)
{
String filename = E. userstate. tostring ();
}

Register event progresschanged to output the project currently being processed. You can operate the UI control in this method.

 

Another method is to isolate the task processing implementation (job function implementation) from a third-party class library. To put it bluntly, it is placed in an external class file, not in the form.CodeObfuscation. Code example: this may be an implementation of the monthly completion function.

Public interface ijob
{
Void execute ();
}

Implementation File

Public class job: ijob
{

Public void execute ()
{

}

}

In the bgworker_dowork method of the form, call job. Execute () to complete the current month.

In this way, the report progress function is a little more complex. You need to use a timer to store the variable values in the progress and update the progress when executing the task.
The implementation steps are as follows. In the interface implementation file, define the set value _ jobprocess variable for storing the progress variable,

Public class job: ijob
{
Private Static concurrentdictionary <int, bool> _ jobprocess;

Initialize the progress variable before the long-running task.

Public void execute ()
{< br> If (_ jobprocess = NULL | _ jobprocess. count = 0 | _ jobprocess. count> 0)
{< br> _ jobprocess = new concurrentdictionary ();
for (INT I = 1; I <= _ step; I ++)
{< br> _ jobprocess. tryadd (I, false);
}< BR >}

_ Step is the step size, which is simplified here. In practice, tasks need to be classified to achieve the goal of step-by-step. For example, the customer's current account is calculated by step based on the customer. There are 121 customers. Here _ step is 121, and each time a customer's account is processed, set it to handled, that is, to call

For (INT I = 1; I <= _ step; I ++)
{
_ Jobprocess [I] = true;
Thread. Sleep (2000 );
}

Each time a task is processed, its time is marked as true to indicate that it has been processed. In this way, the results can be fed back in a timely manner in the progress feedback interface.

Public object [] getexecutestatus ()
{

// Process completed

int completedcount = (from item in _ jobprocess
where item. value = true
select item). Count ();

// Current total task count
Int totalcount = (from item in _ jobprocess
Select item). Count ();

// The task being processed

int iteminprocess = (from item in _ jobprocess
where item. value = false
orderby item. key
select item. key ). firstordefault ();

}

Return the three values to the tick event of the timer interface, and change the progress bar status in real time to report the progress.

 

It takes a certain amount of time to complete the function. Generally, if it is set to more than 20 seconds, backgroundworker should be used for processing to keep the interface responding in a timely manner.

 

2. No interface interaction is required. After the background operation is complete, the result is displayed. The workerthreadbase mode is applied.

The main implementation class of this mode is workerthreadbase,

Public abstract class workerthreadbase: idisposable
{
Private thread _ workerthread;
Private manualresetevent _ stopping;
Private manualresetevent _ stopped;
Private bool _ disposed;
Private bool _ disposing;

Manualresetevent is used to synchronize multiple threads. The meaning of manualresetevent in msdn is to notify one or more threads that an event has occurred. if you cannot understand what it means, run the test code to check its purpose.

Dummyworker = new dummyworker ();
Dummyworker. Start ();

Copyfileworker = new copyfileworker (_ copyinfo );
Copyfileworker. Start ();

// Wait for the two threads to finish
Workerthreadbase. waitall (copyfileworker, dummyworker );

As shown in the code, create two worker threads. The method for creating a worker thread is as follows:

Public class dummyworker: workerthreadbase
{
Protected override void work ()
{

}

}

Inherited from the workerthreadbase type, you can override the work method.

When workerthreadbase. waitall is called to stop the current thread, wait until all threads have finished running, and then the result is displayed.

In summary, the latter mode is simple in programming and more efficient. In winform applications, it is often used to dynamically create backgroundworker in forms methods and then call it, to ensure that the UI continues to accept user input.

 

Go to epn.codeplex.com to downloadSource code.

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.