C# thread.name在代码中给我两个不同的答案
我有两种方法退出这个计划 第一:C# thread.name在代码中给我两个不同的答案,c#,multithreading,winforms,C#,Multithreading,Winforms,我有两种方法退出这个计划 第一: namespace FirstTheard { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.StartPosition = FormStartPosition.CenterScreen; } private void
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);
}
}
}
}
namespace FirstTheard
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
this.StartPosition=FormStartPosition.CenterScreen;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
线程T1=新线程(新线程开始(DoWork));
T1.Name=“Primery螺纹”;
T1.Start();
}
私房
{
var threadName=Thread.CurrentThread.Name;
对于(int i=0;i
{
label1.Text+=“ThreadName为----”+ThreadName+“\n”;
}));
睡眠(100);
}
}
}
}
输出:ThreadName是------主线程 第二:
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);
}
}
}
}
namespace FirstTheard
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
this.StartPosition=FormStartPosition.CenterScreen;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
线程T1=新线程(新线程开始(DoWork));
T1.Name=“Primery螺纹”;
T1.Start();
}
私房
{
//var threadName=Thread.CurrentThread.Name;
对于(int i=0;i
{
label1.Text+=“ThreadName为----”+Thread.CurrentThread.Name+“\n”;
}));
睡眠(100);
}
}
}
}
输出:ThreadName是------- 为什么两个输出不同
请帮助我
Thread.CurrentThread.Name
提供读取此属性的线程的名称
在第一种情况下,在创建的线程T1
上访问(读取)此属性。
在第二种情况下,您可以访问UI线程上的属性(因为通过
Invoke
调用)。由于没有为主UI线程设置任何名称,因此该属性返回一个空字符串。Invoke
将控制权返回到UI线程。在第一个示例中,您正在访问线程.CurrentThread.name
在线程t1
中,即t1.Name
然而,在第二个示例中,您正在访问Invoke
中的Thread.CurrentThread.Name
,在本例中,它将是没有任何名称的主GUI/事件线程。记住,将在拥有控件底层窗口句柄的线程上执行指定的委托。我想我理解了
谢谢大家
这代码表明了我的意思
namespace FirstTheard
{
公共部分类Form1:Form
{
公共字符串线程;
公共表格1()
{
初始化组件()
this.StartPosition=FormStartPosition.CenterScreen;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
//线程的位置现在是“Primery线程”
CurrentThread=Thread.CurrentThread.Name=“Primery Thread”;
label1.Text=CurrentThread+“\n”;
线程T1=新线程(新线程开始(DoWork));
T1.Name=“第二线程”;
T1.Start();
}
私房
{
//现在线程的位置是“次线程”或T1
var threadName=Thread.CurrentThread.Name;
对于(int i=0;i
{
//线程的位置现在又是“原始线程”
label1.Text+=“T1处的ThreadName为----”+ThreadName++++“调用中的ThreadName为----”+Thread.CurrentThread.Name++“\n”;
}));
睡眠(100);
}
}
}
}因为
Invoke
将在不同的线程中运行您的代码片段。
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);
}
}
}