C# Winform中Invoke的用法总结

 

作者:CrazyEditor   来源:CrazyEditor.cn  
 
 
 
 

Windows 窗体中的控件被绑定到特定的线程,不具备线程安全性 。因此,如果从另一个线程调用控件的方法,那么必须使用控件的一个 Invoke 方法来将调用封送到适当的线程。

在设计中为了让界面与逻辑分离,我的做法是使用事件,界面只要响应事件来处理界面的显示就行了。而事件在逻辑处理中可能由不同的线程引发,这些事件的响应方法在修改界面中的控件内容时便会引发一个异常。这时就用到了Control.InvokeRequired 属性 与Invoke方法。
如果当前线程不是创建控件的线程时,InvokeRequired的值为true;否则为false。换句话说,如果InvokeRequired==true表示其它线程需要访问控件,那么调用invoke来转给控件owner处理。

实例一:
首先定义一个委托,如:

 private delegate void InvokeCallback(string msg);

然后判断这个属性的值,来决定是否要调用Invoke函数:

 void MessageEvent(string msg)
 {
   if (txtBox1.InvokeRequired)
     {
        InvokeCallback  msgCallback = new InvokeCallback(MessageEvent);
        txtBox1.Invoke(msgCallback, new object [] { msg } );
     } 
  else 
    {
        txtBox1.Text = msg;
     } 
 }

说明:这个函数就是事件处理函数,txtBox1是一个文本框。这样就做到了窗体中控件的线程安全性。

 

实例二:
public void ButtonOnClick(object sender,EventArgs e)

{

    this.Invoke(new Action(()=>

    {

        button.Text="关闭";

    }));

}
 

以上写法往往充斥着WinForm构建的程序。

在微软新一代的界面开发技术WPF中,由于界面呈现和业务逻辑原生态地分开在两个线程中,所以控件的事件响应函数就不必Invoke了。但是,如果手动开辟一个新线程,那么在这个新线程中改变控件的外观,则还是要Invoke的。

 

使用SynchronizationContext的Post/Send方法更新主线程UI

SynchronizationContext类在System.Threading命令空间下,可提供不带同步的自由线程上下文,其中Post方法签名如下:

public virtual void Post(SendOrPostCallback d,Object state)    //将异步消息调度到一个同步上下文

可以看出我们要异步更新UI控件,第一是要获取UI线程的上下文了,第二就是调用post方法了,代码实现:
实例三:
 

  1. SynchronizationContext syncContext = null;  
  2. //窗体构造函数     
  3. public Form1()  
  4. {  
  5.     InitializeComponent();  
  6.       //获取UI线程同步上下文  
  7.     syncContext = SynchronizationContext.Current;  
  8. }  
  9. private void button1_Click(object sender, EventArgs e)  
  10. {  
  11.     Thread demoThread =new Thread(new ThreadStart(threadMethod));  
  12.     demoThread.IsBackground = true;  
  13.     demoThread.Start();//启动线程  
  14. }  
  15. private void threadMethod()  
  16. {  
  17.      syncContext.Post(SetLabelText, "修改后的文本");//子线程中通过UI线程上下文更新UI  
  18. }  
  19. private void SetLabelText(object text)  
  20. {  
  21.     this.lable1.Text = text.ToString();  
其他invoke使用语法:namespace Interface
{
    // 对主线程的setFrameViewImage进行委托
    delegatevoiddeleteFrameView(IntPtr pFrame);
    classRetriveFrameThread
    {
        privateIntPtr pframe;
        privateRegisterForm m_Form;
        privatedeleteFrameView dispFrameView;
        publicRetriveFrameThread(Form form)
        {
            pframe = IntPtr.Zero;
            m_Form = form asRegisterForm;
            if(form != null)
            {
                dispFrameView = newdeleteFrameView(m_Form.setFrameViewImage);
            }     
        }
        publicvoidrun()
        {
            while(true)
            {
                if(CameraDll.retriveFrame(refpframe) == 0)
                {
                    //dispFrameView(pframe);               直接使用delegate
                    m_Form.Invoke(dispFrameView, pframe);  使用窗体的invoke
                }
                Thread.Sleep(10);
            }
        }
    }
}
posted @ 2020-01-03 15:35  依米儿  阅读(8449)  评论(0)    收藏  举报