Multi-threading and updating User Interface

I have a method that performs some very length execution and i dont wait it to freeze the user interface so i am executing that method in a separate thread.

Thread worker = new Thread(new ThreadStart(DoSomething));
worker.Start();

Now this all works just the way i want, but i understand that a thread should not update things that are owned by other threads, in this situation, the worker thread should not update UI elements in the user interface.

I need to do things like enable/disable buttons, change mouse cursor, report progress etc.
Does the thread have any events like a ThreadComplete event so that i can update the UI to reflect the thread is done



Answer this question

Multi-threading and updating User Interface

  • maxim2000

    Thanks for that.

    I am now looking at using the BackgroundWorker instead now as it supports the events i require.
    I can get the lengthy operation to execute however, the CancelAsync() does not seem to work.

    It does set the CancellationPending property to TRUE, but the DoWork() method does not pick up the CancellationPending value.

    //this is the method tied to the DoWork event
    public int DoLongAction(DoWorkEventArgs e)
    {
    if (worker.CancellationPending)
    {
    e.Cancel = true;
    return 0;
    }
    else
    {
    //web service that takes real long
    ws.DoLongAction();
    return 0;
    }
    return 1;
    }


    private void button3_Click(object sender, EventArgs e)
    {
    worker.CancelAsync();
    }

    //e.Cancelled is always FALSE
    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
    if (e.Cancelled)
    {
    MessageBox.Show("User Cancelled");
    }
    else
    {
    MessageBox.Show("Complete");
    }
    }


    So as you can see by the code snippets, the DoWork method is checking for the cancellation property, but that code doesnt seem to be triggered when i leave a break point.

    When i put a break point on the CancelAsync() i can see that the worker instance cancellation property is TRUE.

    Can someone advise what i am doing wrong here

  • harvs

    Hi,

    you have to check repeatedly in your background thread if CancellationPending is set. If the background thread is in a lengthy call like your DoLongAction it can not be cancelled. If you had more long calls you could check CancellationPending inbetween and acknowledge the cancellation.

    --
    SvenC


  • hnkaraca

    You only checked it once at the beginning of DoLongAction. The problem is, that ws.DoLongAction is a synchronous call where you cannot check and respond to cancellation, only after ws.DoLongAction comes back you can check CancellationPending again. But then everything is done. If you had another ws.DoLongAction2 following you would be able to cancel before calling that method.

    There is no magic way of ending a thread other than tearing it down which leaves the app in an undefined state. Every synchronous call must be completed before you can provide code to end the threads activities. You might have a look at calling webservices asynchronously. That would call the webservice on yet another thread. So that thread will finish the call but you can decide to not wait for its outcome.

    --
    SvenC


  • AIM48

    Have a look at this sample: http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconDevelopingMultithreadedWindowsFormsControl.asp

    It shows how to use delegates and the Invoke or BeginInvoke methods to call delegate handlers in the UI thread.

    --
    SvenC


  • Ludo-R

    SvenC wrote:

    Hi,

    you have to check repeatedly in your background thread if CancellationPending is set. If the background thread is in a lengthy call like your DoLongAction it can not be cancelled. If you had more long calls you could check CancellationPending inbetween and acknowledge the cancellation.

    --
    SvenC



    The code above does show that background thread is checking for the cancellation property.
    So where actually do i place the conditional check on the cancellation property

    I thought placing it in the method that ties to the DoWork() event is adequate because that method gets polled periodically. I am right to think that DoLongAction() method gets polled regualry to check the cancellation property

  • Multi-threading and updating User Interface