关于Control的Invoke方法的使用
2011-11-05 11:47
477 查看
关于Invoke方法的解释,我们首先看看MSDN中详细解释,
翻译:在属于这个控件的窗体的句柄的线程下执行这个指定的代理。
英文水平有限,请大家不要笑啊。
methodType:
System.Delegate(委托类型)
A delegate that contains a method to be called in the control's thread context.
The return value from the delegate being invoked, or Nothing if the delegate has no return value.
返回值为指定的被调用的委托的返回值,如果委托没有返回值则返回空。
在我们使用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;
// ....
}
}
Control.Invoke Method (Delegate)
Executes the specified delegate on the thread that owns the control's underlying window handle.翻译:在属于这个控件的窗体的句柄的线程下执行这个指定的代理。
英文水平有限,请大家不要笑啊。
public Object Invoke( Delegate method )
methodType:
System.Delegate(委托类型)
A delegate that contains a method to be called in the control's thread context.
Return Value
Type: System.ObjectThe 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;
// ....
}
}
相关文章推荐
- 关于Control的Invoke方法的使用
- WinForm 之Control.Invoke 和Control.BeginInvoke 方法的使用 Control 不能在创建它的 Thread 之外被调用。但可以通过 invoke 来保证 C
- 关于 MathMLControl.dll 公式编辑器控件的使用方法
- 在WinFrom应用中,如果使用后台线程来操作界面应调用Control.Invoke()方法
- 关于 MathMLControl.dll 公式编辑器控件的使用方法
- Anthem中Anthem_InvokeControlMethod方法的使用
- 转-关于Method类中invoke方法和Class类中getmethod方法的使用
- WinForm 之Control.Invoke 和Control.BeginInvoke 方法的使用
- WinForm 之Control.Invoke 和Control.BeginInvoke 方法的使用
- 不同线程间通信,使用Control的Invoke和BeginInvoke的方法
- WinForm 之Control.Invoke 和Control.BeginInvoke 方法的使用
- C++ 关于 CMFCPropertyGridCtrl 的使用方法 之一 (原创)
- 关于使用3G无线网卡能上QQ,却打不开网页的处理方法
- 关于崩溃等问题的定位总结(使用qcc的一些方法来尝试)
- 关于System类的使用方法【小程序】
- 3、Python中关于字典的介绍以及使用方法
- IOS-12-关于GCD多线程:dispatch_async 与 dispatch_get_global_queue 的使用方法
- Linux系统平台下关于GCC编译及使用的方法(-Werror,它要求GCC将所有的警告当成错误进行处理 )
- 关于Swift2.0提供的try catch异常捕捉的使用方法
- 关于怎样使用Picture Control显示BMP图片