从一个工作线程调用UI线程死锁时, [英] Deadlock when invoking the UI thread from a worker thread

查看:169
本文介绍了从一个工作线程调用UI线程死锁时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个死锁时,我调用UI线程的工作线程。事实上,工作线程被阻塞在调用行:

I have a deadlock when I invoke the UI thread from a worker thread. Indeed, the worker thread is blocked on the invoke line:

return (ucAvancementTrtFamille)mInterfaceTraitement.Invoke(d, new object[] { psFamille });

奇怪的是,UI线程(其中,纠正我,如果我错了,是主线程)处于闲置状态。

The weird thing is that the UI Thread (which, correct me if I'm wrong, is the main thread) is idle.

有没有什么办法:

  1. 在看哪个线程我确实在尝试调用?
  2. 在看什么说的线程是真正在做什么?

我们可以在下面挡在了调用行的形象,工作线程(ID 3732)中所看到的,而MainThread空闲应用程序的主要功能。

We can see in the image below, the worker thread (ID 3732) blocked on the Invoke line, and the MainThread is idle in the main function of the application.

编辑:这是主线程的堆栈:

Here is the stack of the main thread:

EDIT2:其实,我停下了程序中的第二次,这里是堆栈的样子:

Actually, I paused the the program a second time, and here is what the stack looks like:

EDIT3:解决方法中

我终于找到了解决办法。问题显然是由于异步包装种族 条件问题。解决方法是使用的BeginInvoke,并为它等待一个超时。如果超时,再次调用它,并循环直到最后返回。在大多数情况下,它的实际工作的第二个电话。

I finally found a workaround. The problem is apparently due to an async wrapper race condition issue. The workaround is to use BeginInvoke and wait for it with a timeout. When it times out, invoke it again and loop until it finally returns. Most of the time, it actually works on the second call.

IAsyncResult ar = mInterfaceTraitement.BeginInvoke(d, new object[] { psFamille });
            while (!ar.AsyncWaitHandle.WaitOne(3000, false))
            {
                ar = mInterfaceTraitement.BeginInvoke(d, new object[] { psFamille });
            }
            // Async call has returned - get response
            ucAvancementTrtFamille mucAvancementTrtFamille = (ucAvancementTrtFamille)mInterfaceTraitement.EndInvoke(ar);

这不是pretty的,但是这是我发现的唯一的解决方案。

It's not pretty but it's the only solution I found.

推荐答案

在主线程看起来不闲着。您的屏幕截图显示了它当前的位置在ECM.Program.Main。这不可能是正确的,如果它是空闲的,然后它里面Application.Run(),抽消息循环。这是需要调用()来完成。

The Main thread doesn't look idle. Your screen shot shows it current location at ECM.Program.Main. That can't be correct, if it is idle then it is inside Application.Run(), pumping the message loop. Which is required for Invoke() to complete.

双击主线程,并切换到调用堆栈窗口,了解它是什么真的做。

Double-click the main thread and switch to the Call Stack window to find out what it is really doing.

这篇关于从一个工作线程调用UI线程死锁时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆