thread.name在我的代码中给出了两个不同的答案

时间:2017-09-19 12:41:24

标签: c# multithreading winforms

我有两种方法退出计划。

首先:

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);
            }

        }
    }
}
  

输出:
  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);
            }

        }
    }
}
  

输出:
  ThreadName是-------

为什么两个输出不同?

请帮帮我

4 个答案:

答案 0 :(得分:7)

Thread.CurrentThread.Name为您提供读取此属性的线程的名称。

在第一种情况下,您可以在创建的线程T1上访问(读取)此属性。 在第二种情况下,您可以在UI线程上访问该属性(因为通过Invoke进行调用)。由于您没有为主UI线程设置任何名称,因此该属性返回一个空字符串。

答案 1 :(得分:1)

Invoke将控制权返回给UI线程。

答案 2 :(得分:1)

在第一个示例中,您正在访问Thread.CurrentThread.Name  在线程t1中,即t1.Name

然而,在第二个示例中,您正在访问Thread.CurrentThread.Name中的Invoke,在这种情况下,它将是主GUI /事件线程,它没有任何名称。请记住,Invoke将在拥有控件底层窗口句柄的线程上执行指定的委托。

答案 3 :(得分:0)

我想我明白了 谢谢大家

此代码显示我的意思

namespace FirstTheard

{     公共部分类Form1:表格     {         public string CurrentThread;         公共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);
        }

    }

}

}