Dan Burgener
Dan Burgener

Reputation: 836

Using UI Thread when running WCF

I am attempting to use WCF to test my program. The problem I am running into is when I call methods through WCF, they are run on a worker thread. The method that I am trying to test needs to run in the UI thread or I get the following error:

DragDrop registration did not succeed. Current thread must be set to single thread apartment STA mode before OLE calls can be made. Ensure that your main function has STAThreadAttribute marked on it.

My main function in my program has the STAThread attribute. 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?

Upvotes: 0

Views: 511

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273179

Is there a way to get WCF to run the methods on the UI thread everytime?

No. Even when you config WCF to run as SingleThreaded it will still be a server thread.

Upvotes: 1

Related Questions