C#中只使用Invokerequired来判断是不是UI线程可靠吗?

今天遇到一个C#的Crash,用windbg打开dump,加载sos之后一看,在4号线程出了一个System.InvalidOperationException,在这个地址上调用!pe。可以看到如下的异常信息:

1
2
3
4
5
Exception object:
Exception type: System.InvalidOperationException
Message: The calling thread cannot access this object because a different thread owns it.
InnerException: <none>
StackTrace (generated):

这个异常很常见了,就是非UI线程操作UI控件导致的。我们知道在C#中只有UI线程才能操作UI控件,如果一个工作线程操作UI控件,就会抛这个异常。通常的解决办法很简单,就是使用Invokerequired。如果返回True,说明不在UI线程,需要调用Invoke或者BeginInvoke来扔给UI线程操作。

可是打开call stack对应的代码,已经检查过Invokerequired了!!!这是怎么回事,是说Invokerequired不靠谱吗?

于是又回到MSDN的Invokerequired,找到了如下的说明:

This means that InvokeRequired can return false if Invoke is not required (the call occurs on the same thread), or if the control was created on a different thread but the control’s handle has not yet been created.

You can protect against this case by also checking the value of IsHandleCreated when InvokeRequired returns false on a background thread.

这就清楚了,如果Handle还没有创建好,那么即使在后台工作线程,Invokerequired也返回False。用如下的一个小程序来测试一下,代码很简单,就是在一个Form的构造函数里创建一个Timer,然后在这个Timer的Callback里面打印InvokerequiredIsHandleCreated的值。请注意这里一定要用System.Threading.Timer,因为Form里的那个Timer是运行在UI线程里的,不能用,具体参见我的博客C#中5种timer的比较。另外关于这个System.Threading.Timer有一个坑,参见博客谁动了我的timer?C#的垃圾回收和调试。关于System.Timers.Timer,也有一个坑,参见我的博客.NET 2.0的Timer elapsed event 会自动catch住所有的exception

using Timer = System.Threading.Timer;

namespace Form1
{
    public partial class Form1 : Form
    {
        private Timer m_Timer;
        public Form1()
        {
            m_Timer = new Timer(timer_callback, null, 0, 1);
            InitializeComponent();
        }

        private void timer_callback(object state)
        {
            Debug.WriteLine("IsHandleCreated: {0}", IsHandleCreated);
            Debug.WriteLine("InvokeRequired: {0}", InvokeRequired);
            Debug.WriteLine("Thread ID: {0}", Thread.CurrentThread.ManagedThreadId);
        }
    }
}

输出如下:(这个输出比较随机)

IsHandleCreated: False
InvokeRequired: False
Thread ID: 5

所以正确的写法是:

if(control.IsDisposed || !control.IsHandleCreated)
{
    // UI is not ready, do something special here!!!
    return;
}

if(control.InvokeRequired)
{
    control.Invoke(action);
}
else
{
    action();
}


转载自:

http://fresky.github.io/2014/05/14/is-invokerequired-reliable/


本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2025年4月    »
123456
78910111213
14151617181920
21222324252627
282930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864