Executes the specified delegate on the thread that owns the control's underlying window handle.
翻译:在属于这个控件的窗体的句柄的线程下执行这个指定的代理。
英文水平有限,请大家不要笑啊。
public Object Invoke(
Delegate method
)
返回值为指定的被调用的委托的返回值,如果委托没有返回值则返回空。
在我们使用Control.Invoke方法时,我们经常会用到
MSDN上解释如下:True if the control's Handle was created on a different thread than the calling thread (indicating that you must make calls to the control through an invoke method); otherwise, false.
下面我们来看一个Demo代码:
/// <summary>
/// 委托调用处理页面对温度的调节
/// </summary>
/// <param name="strTemp">温度</param>
private delegate void InvokeCallBack(string strTemp, bool flage);
/// <summary>
/// 处理页面对温度的调节
/// </summary>
/// <param name="strTemp">温度</param>
/// <param name="flage"></param>
private void ChangeStockTemp(string strTemp, bool flage)
{
//酒柜温度调节
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>
/// 设置的温度
/// </summary>
/// <param name="sdp"></param>
private void SetStockTemp(SocketDataPack sdp)
{
//获取要设置的温度,并设置
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);
//输出的调试字符串
strOuput = string.Format("在网页修改温度成功,新设置温度为:{0}\n", sdp.Content);
//将信息写入到日志输出文件
DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
sdpStockTemp = SocketComm.PackSocketData(stockTemp, 0x00, 0x02);
}
else
{
//输出的调试字符串
strOuput = string.Format("发送修改温度的信息的内容的格式错误:{0}\n", sdp.Content);
//将信息写入到日志输出文件
DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
sdpStockTemp = SocketComm.PackSocketData(stockTemp, 0xff, 0x02);
}
}
/// <summary>
/// 处理接收到的Socket消息
/// </summary>
/// <param name="sdp"></param>
private void ProcessSocketMessage(SocketDataPack sdp)
{
switch (sdp.CmdWord)
{
case 0x02:
//设置温度,并发送设置好温度后的状态
SetStockTemp(sdp);
break;
// ....
}
}