thread.join() hangs GUI

A

Asad Khan

I call the following method from my main form method:

uploadThread = new Thread(new ThreadStart
(this.doFTPUpload));
uploadThread.Name = "FTP Upload";
uploadThread.Start();
bool threadTerminatedGood =
uploadThread.Join(client.transmissionTimeout*60*1000);
if (!threadTerminatedGood) {
throw new Exception("The upload thread was interrupted by main thread
because it timed out");
}
// some more code

This works great and does what its supposed to do. However, it hangs my
GUI until the thread exits (either by itself or by exceeding the
allocated timeout). What I want to do is for it to start the thread and
wait for the thread to finish or timeout. While its running or timing
out, if the user clicks a stop button, the thread should get killed
immediately. However, this is not possible, as the GUI just becomes
non-interactive (I presume because it goes into a sleep mode because of
the join call) while the thread runs or times out.

So how can I run the thread, wait for it to finish or timeout, and
meanwhile still be able to respond to user events such as button clicks
etc. (that is keep my GUI from going non-interactive).

Thanks.

Asad
 
M

Markus Stoeger

Asad said:
This works great and does what its supposed to do. However, it hangs my
GUI until the thread exits (either by itself or by exceeding the
allocated timeout). What I want to do is for it to start the thread and
wait for the thread to finish or timeout. While its running or timing
out, if the user clicks a stop button, the thread should get killed
immediately. However, this is not possible, as the GUI just becomes
non-interactive (I presume because it goes into a sleep mode because of
the join call) while the thread runs or times out.

So how can I run the thread, wait for it to finish or timeout, and
meanwhile still be able to respond to user events such as button clicks
etc. (that is keep my GUI from going non-interactive).

Two quick possibilities:

1) tell your thread to quit immediately:

private volatile bool stop;

private void myThread() {
while (!stop) {
// ... do some work
}
}

.... in your GUI set stop = true when you want your thread to exit. Then call
Join(). This way your thread knows that it should exit as soon as possible.

2) use a timer in your GUI to poll the thread's exit status:

private volatile bool done;

private void myThread() {
// ... do some work

done = true;
}

In your GUI you continuously check if done == true and only call Join() when
its true. This way it does not block.

hth,
Max
 
J

Jon Skeet [C# MVP]

Asad Khan said:
I call the following method from my main form method:

uploadThread = new Thread(new ThreadStart
(this.doFTPUpload));
uploadThread.Name = "FTP Upload";
uploadThread.Start();
bool threadTerminatedGood =
uploadThread.Join(client.transmissionTimeout*60*1000);
if (!threadTerminatedGood) {
throw new Exception("The upload thread was interrupted by main thread
because it timed out");
}
// some more code

This works great and does what its supposed to do. However, it hangs my
GUI until the thread exits (either by itself or by exceeding the
allocated timeout). What I want to do is for it to start the thread and
wait for the thread to finish or timeout. While its running or timing
out, if the user clicks a stop button, the thread should get killed
immediately. However, this is not possible, as the GUI just becomes
non-interactive (I presume because it goes into a sleep mode because of
the join call) while the thread runs or times out.

So how can I run the thread, wait for it to finish or timeout, and
meanwhile still be able to respond to user events such as button clicks
etc. (that is keep my GUI from going non-interactive).

You need to make your upload thread call back to the UI thread to let
it know that it's finished - you mustn't hold up the UI thread itself.

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top