简体   繁体   English

运行WCF时使用UI线程

[英]Using UI Thread when running WCF

I am attempting to use WCF to test my program. 我正在尝试使用WCF测试我的程序。 The problem I am running into is when I call methods through WCF, they are run on a worker thread. 我遇到的问题是,当我通过WCF调用方法时,它们在工作线程上运行。 The method that I am trying to test needs to run in the UI thread or I get the following error: 我尝试测试的方法需要在UI线程中运行,否则会出现以下错误:

DragDrop registration did not succeed. DragDrop注册失败。 Current thread must be set to single thread apartment STA mode before OLE calls can be made. 必须先将当前线程设置为单线程单元STA模式,然后才能进行OLE调用。 Ensure that your main function has STAThreadAttribute marked on it. 确保您的主函数上已标记STAThreadAttribute。

My main function in my program has the STAThread attribute. 我程序中的主要函数具有STAThread属性。 I was able to get it to work by doing the following inside of my method. 通过在我的方法中进行以下操作,我能够使其正常工作。

public void MyMethod(){
if (InvokeRequired) {
   Invoke(new MethodInvoker(MyMethod));
   return;
   }
   //Do stuff
}

I don't want to have to make this change for every method inside of my program. 我不想对程序内部的每个方法都进行此更改。 Is there a way to get WCF to run the methods on the UI thread everytime? 有没有办法让WCF每次都在UI线程上运行方法?

Is there a way to get WCF to run the methods on the UI thread everytime? 有没有办法让WCF每次都在UI线程上运行方法?

No. Even when you config WCF to run as SingleThreaded it will still be a server thread. 不会。即使将WCF配置为以SingleThreaded运行,它仍将是服务器线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM