I have a console app that executes a class method (in a different project). This method performs a POST to a webservice asynchronously (this is a lengthy operations of 3 minutes) and has a callback that returns the number of records affected by the POST. In the meantime the program continues doing other things that takes less than 3 minutes and the program exits before the callback returns.
In the past I have used a ManualResetEvent as depicted below to prevent completion but in this case my method is in a different class library that I would like to keep as clean as possible.
static ManualResetEvent resetEvent = new ManualResetEvent(false)
static void Main()
{
CallAsyncMethod();
// Do other things...
resetEvent.WaitOne(); // Blocks until "set"
}
void AsyncMethodCallback()
{
// Do processing on completion...
resetEvent.Set(); // Allow the program to exit
}
I would appreciate any help in coming up with a clean pattern to accomplish this without polluting the called class with execution flags.