I have a method which calls an asynchronous method, and a callback which is fired when the asynchronous method finishes.
I want my method to appear syncrhonous, so I've created an AutoResetEvent, called the asyncrhonous method, the call WaitOne() on the AutoResetEvent instance, and I call Set() in the callback method. Something like this (simplified for this example):
private System.Threading.AutoResetEvent waitRun_m;
public void RunSynchronous()
{
waitRun_m = new System.Threading.AutoResetEvent(false);
CallAsynchronousMethod();
waitRun_m.WaitOne();
}
private void Callback()
{
waitRun_m.Set();
}
Now, is it possible for the call to CallAsynchronousMethod to complete before WaitOne() to be called - causing Set() to be called before WaitOne(). Is there a better way to do this, to avoid this potential issue?