0

I am learning the behavior of GUI Thread vs Worker Thread.
So I create a class which will update GUI Control from different thread ...

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("\n\n\n");

        Thread.CurrentThread.Name = "Parent Thread";
        label1.Text = "I am One";
        System.Diagnostics.Debug.WriteLine("[button1_Click] label1.Text :" + label1.Text);



        Thread _Thred = new Thread(FunctionCallBack);
        _Thred.Name = "Child Thread";
        _Thred.Start();
        _Thred.Join();

        label1.Text = "I am Three";
        System.Diagnostics.Debug.WriteLine("[button1_Click] label1.Text :" + label1.Text);

    }

    void FunctionCallBack()
    {
        MethodInvoker _Method = delegate()
        {
            label1.Text = "I am Two";
            System.Diagnostics.Debug.WriteLine("[FunctionCallBack] label1.Text :" + label1.Text);
        };


        System.Diagnostics.Debug.WriteLine("[FunctionCallBack] label1.BeginInvoke(_Method) : Executed");            
        label1.BeginInvoke(_Method);

        /*
        System.Diagnostics.Debug.WriteLine("[FunctionCallBack] label1.Invoke(_Method) : Executed");
        label1.Invoke(_Method);
        */
    }
}

When I click Button, I get this Output

Output when I using BeginInvoke Method
-------------------------------------- 

[button1_Click] label1.Text :I am One
[FunctionCallBack] label1.BeginInvoke(_Method) : Executed
The thread 'Child Thread' (0xfb8) has exited with code 0 (0x0).
[button1_Click] label1.Text :I am Three
[FunctionCallBack] label1.Text :I am Two

I change my code by using Invoke method instead of calling BeginInvoke method.

System.Diagnostics.Debug.WriteLine("[FunctionCallBack] label1.Invoke(_Method) : Executed");
label1.Invoke(_Method);

I get below output and program cease.

Output when I using Invoke Method 
----------------------------------

[button1_Click] label1.Text :I am One
[FunctionCallBack] label1.Invoke(_Method) : Executed

Please Let me know that, is this situation which I face now call dead lock?
If you say yes , do I have any chance to use Control.Invoke method again ?
Especially , for this kind of situation, do I have only one chance which I have to use Control.BeginInvoke method?

Every suggestion will be appreciated.

Frank Myat Thu
  • 4,172
  • 9
  • 61
  • 112
  • possible duplicate of [What's the difference between Invoke() and BeginInvoke()](http://stackoverflow.com/questions/229554/whats-the-difference-between-invoke-and-begininvoke), [Why would anyone want to use Invoke() (not BeginInvoke())?](http://stackoverflow.com/questions/3940934/why-would-anyone-want-to-use-invoke-not-begininvoke) – Cody Gray Feb 01 '12 at 07:33
  • None of those dupes is specifically about Control.Invoke(), although one of the answers includes them. – Henk Holterman Feb 01 '12 at 08:37

2 Answers2

3

The issue is with your button1_Click method. It's called on the GUI thread and it blocks on _Thread.Join. You should remove this and not wait for the thread to finish; this is the whole idea of executing something on another thread.

The reason why the first case works, is that the worker thread doesn't wait for the GUI thread to update and then returns so that GUI thread is then unblocked.

With the second case, the worker thread waits for the GUI to update, which it can't because it is blocked on wait for the worker to finish. Yes, its a dead lock, but the issue is really with the click method.

Richard Schneider
  • 33,296
  • 8
  • 52
  • 68
1

@RichardSchneider answered why you have a deadlock.

Threading is hard to get right, don't make it more complicated than necessary.

  • Avoid bare Threads, use the ThreadPool or a BackgroundWorker.
  • Usually use Control.BeginInvoke(), but when you call it too often you might swampt the GUI thread. use Control.Invoke() to throttle the background thread. Also it helps to keep UI updates in order.
Henk Holterman
  • 236,989
  • 28
  • 287
  • 464