关于Control的Invoke方法的使用

关于Invoke方法的解释,我们首先看看MSDN中详细解释,

Control.Invoke Method (Delegate)

Executes the specified delegate on the thread that owns the control's underlying window handle.

翻译:在属于这个控件的窗体的句柄的线程下执行这个指定的代理。

英文水平有限,请大家不要笑啊。

public Object Invoke(
	Delegate method
)
method
Type: System.Delegate(委托类型)
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.

返回值为指定的被调用的委托的返回值,如果委托没有返回值则返回空。

在我们使用Control.Invoke方法时,我们经常会用到

Control.InvokeRequired

这个属性来进行判断是否是调用方对该控件进行调用控件,如果不是创建这个控件的线程来调用它,则返回true,否则返回Fale。

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;
// ....

}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值