thread.join() hangs GUI

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Asad Khan

    thread.join() hangs GUI

    I call the following method from my main form method:

    uploadThread = new Thread(new ThreadStart
    (this.doFTPUplo ad));
    uploadThread.Na me = "FTP Upload";
    uploadThread.St art();
    bool threadTerminate dGood =
    uploadThread.Jo in(client.trans missionTimeout* 60*1000);
    if (!threadTermina tedGood) {
    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

    *** Sent via Developersdex http://www.developersdex.com ***
  • Markus Stoeger

    #2
    Re: thread.join() hangs GUI

    Asad Khan wrote:
    [color=blue]
    > 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).[/color]

    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

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: thread.join() hangs GUI

      Asad Khan <asadikhan@gmai l.com> wrote:[color=blue]
      > I call the following method from my main form method:
      >
      > uploadThread = new Thread(new ThreadStart
      > (this.doFTPUplo ad));
      > uploadThread.Na me = "FTP Upload";
      > uploadThread.St art();
      > bool threadTerminate dGood =
      > uploadThread.Jo in(client.trans missionTimeout* 60*1000);
      > if (!threadTermina tedGood) {
      > 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).[/color]

      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/t...winforms.shtml

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      Working...