You already have code that will notify the main thread using the allDone
event. You can put the result in a variable that's visible to the main thread and then call allDone.Set
. When the main thread sees that the event is set, it can then get the result from that variable.
Your primary problem appears to be how to determine if the result is ready. That's a bit of a harder problem and depends on what the main thread is doing. If the main thread is waiting for user input, then you could replace the event and global variable with an event-based asynchronous pattern. The key would be to marshal the event to the UI thread using something like Control.Invoke.
If your main thread is doing something else, then it will have to poll the allDone
event from time to time. Something like:
while (!allDone.WaitOne(0))
{
// do stuff
}
// get and process result
Or:
while (true)
{
if (allDone.WaitOne(0))
{
// get and process result
}
}