Resolve delivery parameter issues by creating a delegate
private void _btnrun_click (object sender, System.EventArgs e)
{
Runtaskdelegate runTask = new Runtaskdelegate (runTask);
Delegate Synchronous Invocation method
RunTask (Convert.ToInt16 (_txtsecond.value));
}
Eliminate blocking problems for user interface threads through delegate asynchronous invocation by creating delegates to resolve delivery parameter issues
private void _btnrun_click (object sender, System.EventArgs e)
{
Runtaskdelegate runTask = new Runtaskdelegate (runTask);
Delegate asynchronous invocation mode
Runtask.begininvoke (Convert.ToInt16 (_txtsecond.value), NULL, NULL);
}
Multithreading security
So far, we have solved the problem of long tasks and the problems of passing parameters. But did we really solve all the problems? The answer is in the negative.
We know that there is a principle in Windows programming that it is not allowed to manipulate forms in any thread other than a form creation thread.
Our program above is the problem: The worker thread is the property of the progress bar that modifies the user interface in the ShowProgress method. Then why does the program run without problems and run normally?
There is no problem because the Windows XP operating system now has a very robust solution to such problems, so that we avoid the problem. However, our current program does not guarantee that the other operating systems can run properly!
The real solution is that we can recognize the problem and avoid it in the process.
How to avoid multi-threaded form resource access security issues? In fact, it is very simple, there are two ways:
One way is not whether the pipeline process is a user interface thread, the user interface resources access is unified by the delegated completion;
Another approach is to have a invokerequired property in each Windows Forms user interface class that identifies whether the current thread has direct access to the form resource. We only need to check the value of this property to access the resource directly if it is allowed to access the form resource directly, otherwise it needs to be accessed through a delegate.
The code snippet that takes the first secure method is as follows:
To display a delegate declaration for a progress bar
delegate void ShowProgressDelegate (int totalstep, int currentstep);
Show progress bar
void showprogress (int totalstep, int currentstep)
{
_progress.maximum = Totalstep;
_progress.value = Currentstep;
}
Delegate declaration to perform a task
delegate void runtaskdelegate (int seconds);
Perform tasks
void RunTask (int seconds)
{
ShowProgressDelegate showprogress = new ShowProgressDelegate (showprogress);
Show progress once every 1/4 seconds
for (int i = 0; i < seconds * 4; i++)
{
Thread.Sleep (250);
Show progress bar
This. Invoke (showprogress, new object[] {seconds * 4, i + 1});
}
}
The code snippet that takes the second safe method is as follows:
//display a delegate declaration for a progress bar
delegate void ShowProgressDelegate (int totalstep, int currentstep);
//Show progress bar
void showprogress (int totalstep, int currentstep)
{
if (_progress.invokerequired)
{
ShowProgressDelegate showprogress = new ShowProgressDelegate (showprogress);
//In order to prevent worker threads from being blocked, use the asynchronous call delegate
this. BeginInvoke (showprogress, new object[] {totalstep, currentstep});
}
Else
{
_progress.maximum = totalstep;
_progress.value = Currentstep;
}
}
//delegate declaration to perform the task
delegate void runtaskdelegate (int seconds);
//Perform task
void RunTask (int seconds)
{
//Show progress every 1/4 seconds
for (int i = 0; i < seconds * 4; i++)
{thread.sleep (250);
//Show progress bar
showprogress (seconds * 4, i + 1);
}
}
At this point, we have used a few examples to illustrate how to perform long tasks, how to asynchronously handle the display of task progress and solve the problem of multithreading security. Hope to give you understanding of multi-threaded programming, the use of delegates, asynchronous calls and other aspects to provide some help, but also hope to be able to communicate and communicate with you further.
C # through threading to control progress bar (GO)--explain the multi-threaded interface operation