我在使用免注册 com 组件时遇到了奇怪的行为。这是我所拥有的:
- Com 组件。我不知道它是为什么线程模型设计的,因为我没有源代码,但它肯定可以在 STA 中工作。
- 该 com 组件的清单 (manifestA)
- c# application (wpf, vs2010, ui thread is sta)
以下代码用于实例化服务器:
var t = new Thread(CreateServer) { Name = "Server", IsBackground = true };
t.SetApartmentState(ApartmentState.STA);
t.Start();
创建服务器如下:
public static void CreateServer()
{
Service = new Service();
Service.Connect();
/* start message queue*/
Dispatcher.Run();
}
wpf应用程序中的代码:
new Thread(() => { GetServer().DoStuff(); }.Start();
因此,当我运行应用程序时,com 方法实际上是在主 STA 线程上运行并阻塞 UI。
现在,如果我在 manifestA 中设置 threadingModel="Apartment" 它不会影响任何东西。但是如果在更改线程模型后重新编译应用程序,com 服务器开始在专用线程上工作。
我搜索了很多,但找不到发生这种情况的原因。为什么 manifestA 只在编译时生效?
有任何想法吗?
谢谢