Reputation: 5
I got two ways out of the program.
First:
namespace FirstTheard
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
}
private void button1_Click(object sender, EventArgs e)
{
Thread T1 = new Thread(new ThreadStart(DoWork));
T1.Name = "Primery Thread";
T1.Start();
}
private void DoWork()
{
var threadName = Thread.CurrentThread.Name;
for (int i = 0; i <= 20; i++)
{
Invoke(new Action(() =>
{
label1.Text += "ThreadName is-------"+threadName+"\n";
}));
Thread.Sleep(100);
}
}
}
}
Output:
ThreadName is-------Primary Thread
Second:
namespace FirstTheard
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
}
private void button1_Click(object sender, EventArgs e)
{
Thread T1 = new Thread(new ThreadStart(DoWork));
T1.Name = "Primery Thread";
T1.Start();
}
private void DoWork()
{
//var threadName = Thread.CurrentThread.Name;
for (int i = 0; i <= 20; i++)
{
Invoke(new Action(() =>
{
label1.Text += "ThreadName is-------"+ Thread.CurrentThread.Name + "\n";
}));
Thread.Sleep(100);
}
}
}
}
Output:
ThreadName is-------
Why are two outputs different?
please help me
Upvotes: 0
Views: 81
Reputation: 5
I think I understood Thank you everybody
This code show my means
namespace FirstTheard
{ public partial class Form1 : Form { public string CurrentThread; public Form1() { InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
}
private void button1_Click(object sender, EventArgs e)
{
//Position of thread now is "Primery Thread"
CurrentThread = Thread.CurrentThread.Name= "Primery Thread";
label1.Text = CurrentThread + "\n";
Thread T1 = new Thread(new ThreadStart(DoWork));
T1.Name = "Secondery Thread";
T1.Start();
}
private void DoWork()
{
//Position of thread now is "Secondary Thread" or T1
var threadName = Thread.CurrentThread.Name;
for (int i = 0; i <= 20; i++)
{
Invoke(new Action(() =>
{
//Position of thread now is "Primery Thread" again
label1.Text += "ThreadName at T1 is-------"+ threadName + " " + "ThreadName in the Invoke is-------" + Thread.CurrentThread.Name+ "\n";
}));
Thread.Sleep(100);
}
}
}
}
Upvotes: 0
Reputation: 4394
In the first sample, you are accessing the Thread.CurrentThread.Name
in the Thread t1
i.e. the t1.Name
Whereas, in the second sample, you are accessing the Thread.CurrentThread.Name
within Invoke
, which, in this case will be the main GUI/Event thread, which does not have any name. Remember, Invoke
will execute the specified delegate on the thread that owns the control's underlying window handle.
Upvotes: 1
Reputation: 15197
Thread.CurrentThread.Name
gives you the name of the thread which reads this property.
In the first case, you access (read) this property on your created thread T1
.
In the second case, you access the property on the UI thread (because of calling via Invoke
). And since you didn't set any name for the main UI thread, the property returns an empty string.
Upvotes: 7