Gogolo
Gogolo

Reputation: 128

Cross-thread operation not valid

Dears, I'm getting error message bellow when I try to call a sort method:

Error Message: Cross-thread operation not valid: Control 'lbStart_Bubble' accessed from a thread other than the thread it was created on.

What is here done uncorrectly!?

Thread thBubble = new Thread(new ThreadStart(bubbleSort));
thBubble.Start();


public void bubbleSort()
    {
        int row = 0, column = 0;

        start = new TimeSpan(System.DateTime.Now.Ticks);
        lbStart_Bubble.Text = start.ToString();
        this.lbStart_Bubble.Refresh();

        for(row = 1; row <= list1.Length; row++)
        {
            for(column =0; column < list1.Length-1; column++)
            {

                Thread.Sleep(delay);
                tbResult_Bubble.Clear();
                for(int i=0; i<list1.Length; i++)
                    tbResult_Bubble.AppendText(list1[i] + " " );

                if(list1[column]>list1[column+1])
                    swap(list1[column], list1[column+1], column, column+1);
            }
            display(list1);
        }

        end = new TimeSpan(System.DateTime.Now.Ticks);
        lbEnd_Bubble.Text = end.ToString();

        lbTotal_Bubble.Text = end.Subtract(start).ToString();

        tbResult_Bubble.Clear();

        for(int i=0; i<list1.Length; i++)
            tbResult_Bubble.AppendText(list1[i] + " " );

    }

Upvotes: 2

Views: 1845

Answers (2)

Felice Pollano
Felice Pollano

Reputation: 33252

You can't drive the user interface by a thread differend to the one that created it. Have a look at Control.Invoke and use it to drive the UI from your background thread.

Upvotes: 1

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391336

You cannot talk to controls from a different thread than the one that created and thus owns that control.

So for one, your usage of the label cannot be done like that.

Instead, you can Invoke or BeginInvoke:

lbStart.Invoke(new Action(() =>
{
    lbStart_Bubble.Text = start.ToString();
}));

or:

lbStart.BeginInvoke(new Action(() =>
{
    lbStart_Bubble.Text = start.ToString();
}));

The difference is that the first will wait until the main thread (the one that owns the label) has executed the code before it (the background thread) continues executing. Think of it has a regular method call, it just happens on a different thread.

The second will just send off a mail to the thread that owns the label, asking it to execute the piece of code, and then not wait for it. This can be tricky to get right, for instance what if start changes in the background thread before the main thread has gotten around to executing that code?

I would use Invoke until you get more experience with threading and then look into alternatives.

Or, you can use BackgroundWorker which has facilities to safely send progress messages back to the main thread.

Upvotes: 8

Related Questions