关于在 C# (WPF) 中对 COM 对象进行异步调用的问题
很抱歉问这么基本的问题,但我似乎在这个问题上脑子冻结了!我正在从我的 WPF 项目中调用 COM (ATL) 对象。 COM 方法可能需要很长时间才能完成。我想我应该尝试异步调用它。我有一些演示行可以显示问题。
private void checkBox1_Checked(object sender, RoutedEventArgs e)
{
//DoSomeWork();
AsyncDoWork caller = new AsyncDoWork(DoSomeWork);
IAsyncResult result = caller.BeginInvoke(null, null);
}
private delegate void AsyncDoWork();
private void DoSomeWork()
{
_Server.DoWork();
}
ATL 方法DoWork 非常令人兴奋。它是:
STDMETHODIMP CSimpleObject::DoWork(void)
{
Sleep(5000);
return S_OK;
}
我期望以这种方式运行会导致立即选中复选框(而不是 5 秒内),并且我能够在屏幕上移动 WPF gui。我不能——5秒钟。
我做错了什么?我确信这很简单。代表签名错误?
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我确信您对 ATL 代码的调用被编组到 GUI 线程是正确的,因为 ATL 代码是 STA,从而阻塞了 GUI 线程。
两种解决方案:
WPF 应用程序实际上可以在多个 UI 线程中正常运行,只要每个 UI 线程管理自己的 UI 部分,并且这些部分由 HwndSource 分隔。换句话说,运行部分 UI 的第二个线程实现 Win32 HWND,然后将其嵌入到主线程运行的 UI 部分中。
如果您的 COM 对象本身不是 GUI 对象,那么应该很容易在单独的工作线程中构造它并将其留在那里。由于它是 STA 对象,因此所有调用都将编组到其他线程。
I'm sure you're right about the call to your ATL code getting marshaled to the GUI thread because the ATL code is STA, thereby blocking your GUI thread.
Two solutions:
A WPF application actually runs just fine with multiple UI threads, as long as each UI thread has manages its own part of the UI, and the parts are separated by HwndSource. In other words, the second thread that runs part of the UI implements a Win32 HWND which is then embedded in the portion of the UI run by the main thread.
If your COM object isn't itself a GUI object, then it should be very easy to construct it in a separate worker thread and leave it there. Since it is a STA object, all calls will be marshaled to the other thread.
BeginInvoke
仍将在同一线程上执行您的调用,只是异步*。您可以创建一个新的 Thread 对象:或者尝试 .Net 4 的新 任务库:
本质上是相同的东西。**
*委托类型的
BeginInvoke
方法与调用者在同一线程上执行,但在后台执行。我不确定是否有关于何时执行什么的规则,但它肯定不是按照您想要的顺序。但是,像BeginRead
这样的异步方法在与主线程分开的特殊线程上执行。**有一点细微的差别 - Thread 方法总是创建一个新的 Thread 对象,而 Task 系统有一个线程池可以使用,这在理论上是可行的更有效率。
BeginInvoke
is still going to execute your call on the same thread, just asynchronously*. You can either create a newThread
object:or try out .Net 4's new Task library:
which are essentially the same thing.**
*A delegate type's
BeginInvoke
method executes on the same thread as the caller, but in the background. I'm not sure if there are rules regarding what gets executed when, but it's certainly not in the order you want. However, asynchronous methods likeBeginRead
execute on a special thread separate from the main one.**There is a slight difference - the Thread method will always create a new
Thread
object, whereas theTask
system has a pool of threads to work with, which is in theory more efficient.我对此做了更多的思考和测试。 C#代码没有任何问题。如果 ATL 对象是 STA 对象(就像我的例子一样),它将在主线程上调用,无论 C# 代码尝试在工作线程上调用它。将 ATL 对象更改为 MTA 对象使得可以异步调用它。
I have done some more thinking and testing about this. There is nothing wrong with the C# code. If the ATL object is an STA object (as it was in my case), it will be called on the main thread, regardless of attempts by the C# code to call it on a worker thread. Changing the ATL object to an MTA object makes it possible to to call it asynchronously.