Assume I have method void SomeMethod(Action callback) This method does some work in background thread and开发者_StackOverflow社区 then invokes callback. The question is - how to block current thread until callback is called ?
There is an example
bool finished = false;
SomeMethod(delegate{
finished = true;
});
while(!finished)
Thread.Sleep();
But I'm sure there should be better way
You can use AutoResetEvent to signal when your thread is finished.
Check this code snippet:
AutoResetEvent terminateEvent = new AutoResetEvent(false);
bool finished = false;
SomeMethod(delegate
{
terminateEvent.Set();
});
terminateEvent.WaitOne();
Check for Thread.Join()
will work
Example of this
精彩评论