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.