In general, we need to display the prompt information on the main interface from time to time in other business classes. The following method can be used:
Form1.cs
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. LINQ;
Using system. text;
Using system. Threading;
Using system. Windows. forms;
Namespace windowsformsapplication2
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private void form1_load (Object sender, eventargs E)
{
Testdelegate TD = new testdelegate ();
TD. cb = safesettext;
Thread th = new thread (New threadstart (TD. Test ));
Th. Start ();
}
Public void safesettext (string text)
{
If (this. invokerequired)
{
_ Safesettextcall call = delegate (string S)
{
This. label1.text = s;
};
This. Invoke (call, text );
}
Else
This. label1.text = text;
}
Public Delegate void _ safesettextcall (string text );
}
}
Testdelegate. CS
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Threading;
Namespace windowsformsapplication2
{
Public Delegate void callback (string MSG );
Public class testdelegate
{
Public callback CB;
Public testdelegate ()
{
}
Public void test ()
{
For (INT I = 0; I <100; I ++ ){
CB (I. tostring ());
Console. writeline (I. tostring ());
Thread. Sleep (1000 );
}
}
}
}
Delegate to modify the control attributes in form in other classes