C # Send information to the main thread by delegate

c# Windows programming often uses multiple threads. When you want to change the page data of the main thread in the newly opened thread, you need to delegate through the delegate

 public delegate void  show(string info);  //Define a delegate, the parameter is string private void add_info_event(string info) //define one Method, determine whether the control needs to be referenced to operate, bind the method with the delegate, and use Invoke to call the delegate and pass parameters. 

{
if (this.txb_info.InvokeRequired)
{
show d
= new show(add_info_event); //Declare a show delegate, and the delegate points to the add_info_event methodthis.Invoke(d, new object[] {info }); //quote the delegate and pass parameters
}
else
{
add_info(info); //Call the real code logic
}
}

In the newly opened thread code, directly calling add_info() is a cross-thread operation and cannot control the content of the main thread, just call add_info_event().

Extension: If the main form generates a sub-form, the sub-form must communicate with the main form To change the content, you can also use delegate and event to cooperate.

Subform1:

 public delegate void sendMessage(string message); //Define a delegate and send a messagepublic event sendMessage sendTo; //Declare an event, the type is sendMessage, call sendTo(string message) to communicate with the main form

< /div>

Main form:

Form1 form1=new Form1();
//The construction method of the main form
public Form(){
 form1.show();
form1.sendTo
+= new Form1.sendMessage(SendArgs); < span style="color: #008000;">//The sendTo event type of the subform is delegated to sendMessage, and the delegate is bound to the SendArgs method in the main form;
}

public delegate void getMessage(string message); //Define a delegate to receive messagesprivate async void SendArgs(string message)
{
if (this.DateListView.InvokeRequired)  //Determine whether the control needs to be referenced
{
getMessage d
= new getMessage(SendArgs); //Declare a getMessage delegate, which points to the SendArgs method. this.Invoke(d, new object[] {message}); //call the delegate , Pass parameters
}
else {
MessageBox.Show(message
);
}
}

The child form sends a message, a delegate, an event sendTo

The main form receives a message, a Delegation

Both delegates point to the SendArgs method of the main form

The subform triggers the sendTo event, that is, the sendMessage delegate is called, and the sendArgs method is executed. The sendArgs method determines that the control needs to be referenced to change Execute, call the getMessage delegate;

 public  delegate void show(string info); //Define a delegate, the parameter is string private void add_info_event(string info) //Define a method to determine whether the control needs to be referenced to operate, bind the method with the delegate, and use Invoke to call the delegate and pass parameters. 

{
if (this.txb_info.InvokeRequired)
{
show d
= new show(add_info_event); //Declare a show delegate, and the delegate points to the add_info_event methodthis.Invoke(d, new object[] {info }); //quote the delegate and pass parameters
}
else
{
add_info(info); //Call the real code logic
}
}

 public delegate void sendMessage(string message); //Define a delegate, send a messagepublic event  sendMessage sendTo; //Declare an event, the type is sendMessage, call sendTo(string message) to communicate with the main form

Form1 form1=new Form1();
//The main form construction method
public Form(){
 form1.show();
form1.sendTo
+= new Form1.sendMessage(SendArgs); < span style="color: #008000;">//The sendTo event type of the subform is delegated to sendMessage, and the delegate is bound to the SendArgs method in the main form;
}

public delegate void getMessage(string message); //Define a delegate to receive messagesprivate async void SendArgs(string message)
{
if (this.DateListView.InvokeRequired)  //Determine whether the control needs to be referenced
{
getMessage d
= new getMessage(SendArgs); //Declare a getMessage delegate, which points to the SendArgs method. this.Invoke(d, new object[] {message}); //call the delegate , Pass parameters
}
else {
MessageBox.Show(message
);
}
}

Leave a Comment

Your email address will not be published.