Contents

今天遇到一个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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

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);
}
}
}

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

1
2
3
IsHandleCreated: False
InvokeRequired: False
Thread ID: 5

所以正确的写法是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if(control.IsDisposed || !control.IsHandleCreated)
{
// UI is not ready, do something special here!!!
return;
}

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