Reputation:
I have to call multiple WCF methods in sequence without blocking the UI thread.
Each method have to be completed before the next method is called.
I have setup my WCF service with svcutil so I have an async version of my synchronous methods available.
If I call the async version, the methods will execute simultaneously, what I want to avoid.
If I call the sync version, the UI thread is blocked so my UI is unresponsive, what I want to avoid.
How can I call the async version and wait for each call to complete before the next call, without blocking the UI thread ?
Say I have to loop for each occurence of objects to be treated by a WCF method :
foreach (MyObject obj in SomeCollection)
{
myWCFProxy.TreatObject(obj); // This is a duplex service and I am handling its callback in a separate method.
// I would like to wait here (without blocking the UI thread) until the method returns.
}
How can I do this ?
Upvotes: 0
Views: 1005
Reputation: 56586
If it's an option, I'd suggest modifying the WCF service to take many MyObject
s at once and process them as needed. Then your code on the client becomes:
myWCFProxy.TreatObjects(SomeCollection); //be sure to handle the callback
And on the WCF side:
foreach (var obj in SomeCollection)
TreatObject(obj); //it's synchronous, so it waits!
Upvotes: 0
Reputation: 124804
Based on your description, the simplest solution might be to use a BackgroundWorker to execute the WCF calls synchronously.
Upvotes: 1
Reputation: 21
Couldn't you just wrap a series of synchronous WCF calls in an asynchronous method? This keeps the code from executing on your UI thread yet executes your WCF calls in linear fashion.
Another option would be to call the followup WCF method in the callback of the previous WCF method.
Upvotes: 2