C
cj_junktrap
Hi
I have a method which I want to present as a synchronous call - you
pass in your parameters, and the result as a return value. But within
that method, I need to spin off a thread, wait for it to finish doing
some stuff, and return a value to the calling method. The problem is
that my method may be called on the gui thread (since it's out of my
control where my method is called from), and the gui will become
unresponsive while my method is running if I wait for the thread to
finish using Thread.Join. So what I'm doing at the moment is a loop
like this:
while (Thread.IsAlive)
{
Thread.Sleep(100);
Application.DoEvents();
}
Now I know that using Application.DoEvents like that is horrible, but I
can't see any other way to wait for the thread to finish before
returning the value to the calling method. Sure, I could pass the
thread a delegate to call when it's finished, but I want the whole
process to appear to be synchronous to the calling method. Is there a
better way to accomplish this?
I have a method which I want to present as a synchronous call - you
pass in your parameters, and the result as a return value. But within
that method, I need to spin off a thread, wait for it to finish doing
some stuff, and return a value to the calling method. The problem is
that my method may be called on the gui thread (since it's out of my
control where my method is called from), and the gui will become
unresponsive while my method is running if I wait for the thread to
finish using Thread.Join. So what I'm doing at the moment is a loop
like this:
while (Thread.IsAlive)
{
Thread.Sleep(100);
Application.DoEvents();
}
Now I know that using Application.DoEvents like that is horrible, but I
can't see any other way to wait for the thread to finish before
returning the value to the calling method. Sure, I could pass the
thread a delegate to call when it's finished, but I want the whole
process to appear to be synchronous to the calling method. Is there a
better way to accomplish this?