I have a long running series of operations in a .NET 2.0 BackgroundWorker
thread. When I make a call to unmanaged code located in a referenced assembly the UI is frozen until the call completes.
我在.NET 2.0 BackgroundWorker线程中进行了一系列长时间的操作。当我调用位于引用程序集中的非托管代码时,UI将被冻结,直到调用完成。
Why is this? Should I not be making these calls from the BackgroundWorker
thread?
为什么是这样?我应该不从BackgroundWorker线程进行这些调用吗?
Yes, it is a COM component but I'm not sure how to tell if it's STA or not and what the implications/resolution are/is. I make several calls to that component but it is only the long-running ones where I notice the UI becoming unresponsive.
是的,它是一个COM组件,但我不知道如何判断它是否是STA以及影响/解决方案是什么/是什么。我对该组件进行了几次调用,但只是长时间运行的那些我注意到UI变得无法响应。
These calls retrieve data from a server.
这些调用从服务器检索数据。
3 个解决方案
#1
3
You've got the COM tag in the question - are you calling into a COM component? Is it an STA component? If so, it may well be marshalling back to do its work on the UI thread somehow. I'll readily admit I'm a long way from being an expert on COM stuff, but I wouldn't be surprised if that were the problem.
你在问题中有COM标签 - 你是否正在调用COM组件?它是STA组件吗?如果是这样,很可能会以某种方式组织回来在UI线程上进行工作。我很乐意承认自己在成为COM专家方面还有很长的路要走,但如果这是问题,我也不会感到惊讶。
What happens if you make the calls from a new thread which you've explicitly created?
如果您从明确创建的新线程进行调用会发生什么?
#2
0
Yes and also, does this happen with all unmanaged code or just a specific component or API? If a specific one, what is that unmanaged code doing?
是的,并且,这是否会发生在所有非托管代码或仅特定组件或API上?如果是特定的那个,那个非托管代码在做什么?
#3
0
Also, you could simply comment out your current code in the BackgroundWorker (you are running RunWorkerAsync right...) and put a sleep in there. If your GUI becomes unresponsive something's not right, otherwise it's the code you are calling as Jon points out could be the case with COM.
此外,您可以简单地在BackgroundWorker中注释掉当前代码(您正在运行RunWorkerAsync ...)并在那里进行睡眠。如果你的GUI变得没有响应,那么某些东西是不正确的,否则它就是你正在调用的代码,因为Jon指出COM可能就是这种情况。
#1
3
You've got the COM tag in the question - are you calling into a COM component? Is it an STA component? If so, it may well be marshalling back to do its work on the UI thread somehow. I'll readily admit I'm a long way from being an expert on COM stuff, but I wouldn't be surprised if that were the problem.
你在问题中有COM标签 - 你是否正在调用COM组件?它是STA组件吗?如果是这样,很可能会以某种方式组织回来在UI线程上进行工作。我很乐意承认自己在成为COM专家方面还有很长的路要走,但如果这是问题,我也不会感到惊讶。
What happens if you make the calls from a new thread which you've explicitly created?
如果您从明确创建的新线程进行调用会发生什么?
#2
0
Yes and also, does this happen with all unmanaged code or just a specific component or API? If a specific one, what is that unmanaged code doing?
是的,并且,这是否会发生在所有非托管代码或仅特定组件或API上?如果是特定的那个,那个非托管代码在做什么?
#3
0
Also, you could simply comment out your current code in the BackgroundWorker (you are running RunWorkerAsync right...) and put a sleep in there. If your GUI becomes unresponsive something's not right, otherwise it's the code you are calling as Jon points out could be the case with COM.
此外,您可以简单地在BackgroundWorker中注释掉当前代码(您正在运行RunWorkerAsync ...)并在那里进行睡眠。如果你的GUI变得没有响应,那么某些东西是不正确的,否则它就是你正在调用的代码,因为Jon指出COM可能就是这种情况。