Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF threading - non-responsive UI

I'm trying to configure some WCF stuff. Currently, I have a server which allows remote users to download files, and client. In the server, I use a ServiceHost class. I assume it should be running on a separate thread, however, the server UI (WinForms) becomes locked when someone downloads a file. Is there a way to manage the WCF threading model?

Thank you!

like image 738
SharpAffair Avatar asked Jun 01 '10 11:06

SharpAffair


2 Answers

You should add a ServiceBehaviorAtttribute to the class implementing your service and set its UseSynchronizationContext property to false. This will cause calls to your service to be processed on their own thread.

Example:

[ServiceBehavior(UseSynchronizationContext=false)]
class YourService : IYourService
{
  // Service Methods
}

Just remember that if you are going to update any Controls from within your service methods, you must bear in mind the cross-thread programming model of Windows Forms.

like image 182
luksan Avatar answered Nov 01 '22 16:11

luksan


"From the same Windows Form application if you were to construct the ServiceHost instance before starting the UI thread, it will run on its own thread. That means worker threads allocated from the thread pool process messages instead of the message loop. Thus, services can truly process multiple concurrent requests. "

like image 30
SharpAffair Avatar answered Nov 01 '22 17:11

SharpAffair