I have a BackgroundWorker
that calls an asynchronous method. The async method provides a callback argument to let me know it's finished.
//Bunch of stuff to prep for the call happens first
SomeAsyncMethodIn3rdPartyDll(callback);
//A few more clean up operations after the call
The problem I have is that the BackgroundWorker
finishes before the callback is called and thus it is never called since the thread is dead.
My current (working) solution is to add this at the end of my DoWork
method:
while (keepAlive)
{
Application.DoEvents();
System.Threading.Thread.Sleep(100);
}
Then I simply set the value of keepAlive
to false when I've received the callback.
This seems hacky, especially with the Application.DoEvents()
command which is required to get the callback to fire.
So, what I want to know is: Is there a better way to keep the thread alive? Or, can I specify which messages I want to process instead of DoEvents
(which does all messages)? Some other solution?