For an explanation of the invoke method, Let's first look at the detailed explanation in msdn,
Control. Invoke method (delegate)
Executes the specified delegate on the thread that owns the control's underlying window handle.
Execute the specified proxy in the thread of the form handle of the control.
The English level is limited. Please do not laugh.
public Object Invoke(Delegate method)
-
Method
-
Type:
System. Delegate (delegate type)
A delegate that contains a method to be called in the control's thread context.
Return Value
Type: system. Object
The return value from the delegate being invoked, or nothing if the delegate has no return value.
The return value is the return value of the specified called deleGATE. If the delegate does not return a value, the return value is null.
We often use the control. Invoke method.
Control. invokerequired
This attribute is used to determine whether the control is called by the caller. If the control is not called by the thread that creates the control, true is returned; otherwise, Fale is returned.
The msdn explanation is as follows: true if the control's
Handle was created on a different thread than the calling thread (indicating that you must make callto the control through an invoke method); otherwise,
False.
Here is a Demo code:
/// <Summary>
/// Adjust the temperature on the delegate call processing page
/// </Summary>
/// <Param name = "strtemp"> temperature </param>
Private delegate void invokecallback (string strtemp, bool flage );
/// <Summary>
/// Temperature Adjustment on the processing page
/// </Summary>
/// <Param name = "strtemp"> temperature </param>
/// <Param name = "flage"> </param>
Private void changestocktemp (string strtemp, bool flage)
{
// Wine cabinet Temperature Adjustment
If (lbinnertemp. invokerequired)
{
Invokecallback lblinnertempchangestocktemp = new invokecallback (changestocktemp );
Lbinnertemp. Invoke (lblinnertempchangestocktemp, new object [] {strtemp, flage });
}
Else
{
Int itemp = int. parse (strtemp );
If (itemp> maxtemp)
{
Strtemp = maxtemp. tostring ();
}
Else if (itemp <mintemp)
{
Strtemp = mintemp. tostring ();
}
Lbinnertemp. Text = strtemp;
}
}
/// <Summary>
/// Set the temperature
/// </Summary>
/// <Param name = "SDP"> </param>
Private void setstocktemp (socketdatapack SDP)
{
// Obtain the temperature to be set and set
String stocktemp = SDP. content;
Int itemp = 12;
String strouput = "";
Socketdatapack sdpstocktemp = NULL;
If (Int. tryparse (stocktemp, out itemp ))
{
Dllcomm. tp_settemperature (stocktemp );
Changestocktemp (stocktemp, false );
// Output debugging string
Strouput = string. Format ("the temperature is successfully modified on the webpage, and the new temperature is set to {0} \ n", SDP. content );
// Write the information to the log output file
Dllcomm. tp_writeapplogfileex (dllcomm. g_applogfilename, strouput );
Sdpstocktemp = socketcomm. packsocketdata (stocktemp, 0x00, 0x02 );
}
Else
{
// Output debugging string
Strouput = string. Format ("the format of the content that sent the Temperature modification information is incorrect: {0} \ n", SDP. content );
// Write the information to the log output file
Dllcomm. tp_writeapplogfileex (dllcomm. g_applogfilename, strouput );
Sdpstocktemp = socketcomm. packsocketdata (stocktemp, 0xff, 0x02 );
}
}
/// <Summary>
/// Process received socket messages
/// </Summary>
/// <Param name = "SDP"> </param>
Private void processsocketmessage (socketdatapack SDP)
{
Switch (SDP. Encrypted word)
{
Case 0x02:
// Set the temperature and send the status after the temperature is set
Setstocktemp (SDP );
Break;
//....
}
}