C Method of Implementing Multi thread Operation Based on Delegation
- 2021-08-28 20:53:28
- OfStack
In this paper, an example is given to describe the method of C # to realize the operation between multiple threads based on delegation. Share it for your reference, as follows:
Sometimes we have to have multiple threads, and more often, one thread may manipulate the attributes in other threads.
However, threads are concurrent, and 1-like calls cannot meet our requirements.
So, we can use the delegate here, and the code is as follows
private delegate void DelegateInfo();
private delegate void DelegateIsEnd();
// This is the method that a thread calls other threads
private void Dowork()
{
// Determine whether it is necessary Invoke Required when multithreaded
if (this.InvokeRequired)
{
// Call the program that writes the main line program control by delegate, and pass the parameters in object Array
this.Invoke(new DelegateInfo(LoadFile));
}
else
{
// If no delegate call is required, call directly
this.LoadFile();
}
//========= Thread end ===========
this.BeginInvoke(new DelegateIsEnd(ISEnd));
}
private void ISEnd()
{
wf.Visible = false;
wf.Close();
wf.Dispose();
}
private void LoadFile()
{
}
private WaitingForm wf = nu
I hope this article is helpful to everyone's C # programming.