I have a desktop app (.net 3.5) which communicates with the server using classic web services.
I need to make numerous calls to web service functions, wait for them all to finish, and then proceed.
I’d like to do these calls in parallel; so obviously I’m looking to utilize the MyFunctionAsync method.
The thing is- i’m not sure how to verify that all calls have returned. I’m probably missing some simple synchronization mechanism here.
here’s what i’d like to be able to do:
public void DoWork()
{
service.MyFuncCompleted+= MyFunc_Completed;
foreach (var i in items)
{
service.MyFuncAsync(i);
syncMechanism.WaitForAnother(1); //signal that we have to wait for 1 more task to finish
}
syncMechanism.WaitForAllTasksToFinsih(); //wait until specified number of tasks have finished
//continue
}
public void MyFunc_Completed(EventArgs e)
{
//process result...
syncMechanism.Signal(1); //signal that 1 task has finished
}
anybody know anything like this is c#?
This ( fork / join ) is a lot easier in .NET 4, but you’re on 3.5 so you’ll have to do it the old-fashioned way.
Basically, increment a counter for each operation and then decrement it in your
Completedhandler. Then check if it’s got down to zero and if so signal a wait handle to indicate all operations have completed.Here’s an example of the idea: