zoukankan      html  css  js  c++  java
  • 关于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;
               //  ....

                }
            }





  • 相关阅读:
    PDI的steps:(8:Avro input)
    Expert Cube Development with Microsoft SQL Server 2008 Analysis Services(1)
    PDI的steps:(6:Analytic Query)
    PDI的steps:(5:Add value fields changing sequence)
    Junk dimensions
    PDI的steps:(8:Automatic Documentation Output)
    PDI的steps:(5:Add XML)
    PDI的steps:(7:Append streams)
    Expert Cube Development with Microsoft SQL Server 2008 Analysis Services(2)度量值和度量值组
    Expert Cube Development with Microsoft SQL Server 2008 Analysis Services(3) 第一章
  • 原文地址:https://www.cnblogs.com/kevinGao/p/2238889.html
Copyright © 2011-2022 走看看