Csharp中Thread的四种构造函数

  • 带参数的构造函数

第一种:Thread(ParameterizedThreadStart)

 private void treeView1_NodeMouseClick()
        {
            try
            {
                TreeNode tn = new TreeNode();
                tn = e.Node;

                Thread th = new Thread(new ParameterizedThreadStart(URLshow));
                th.IsBackground = true;
                th.Start(tn);
            }
            catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
        }

        private void URLshow(object tn)
        {
            try
            {
                TreeNode tnn = (TreeNode)tn;
                this.Invoke(new EventHandler(delegate
                    {
                            this.textBox3.Text = FTPIP + tnn.FullPath.ToString().Replace("\\", "/");
                    }));
            }
            catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
        }

第二种:Thread(ParameterizedThreadStart, Int32)

  • 不带参数的构造函数

    第三种:Thread(ThreadStart)

private void treeView1_NodeMouseClick()
        {
            try
            {
                TreeNode tn = new TreeNode();
                tn = e.Node;

                Thread th = new Thread(new ParameterizedThreadStart(URLshow));
                th.IsBackground = true;
                th.Start();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
        }

        private void URLshow()
        {
            try
            {
                this.Invoke(new EventHandler(delegate
                    {
                            this.textBox3.Text = FTPIP + tnn.FullPath.ToString().Replace("\\", "/");
                    }));
            }
            catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
        }

第四种:Thread(ThreadStart, Int32)

总结:
第二种和第四种的代码流程跟第一种和第三种相似,区别在于就多一个参数,该参数代表着线程要使用的最大堆栈大小(以字节为单位);

温馨提示:避免使用此构造函数重载。 使用的默认堆栈大小Thread(ParameterizedThreadStart)构造函数重载是线程的建议的堆栈大小。 如果一个线程出现内存问题,最可能的原因编程错误,例如无限递归。

详细请看:https://msdn.microsoft.com/zh-cn/library/ms149581(v=vs.110).aspx
(Thread(ParameterizedThreadStart, Int32))
详细请看:https://msdn.microsoft.com/zh-cn/library/5cykbwz4(v=vs.110).aspx
(Thread(ThreadStart, Int32))

外附加一种

 private void treeView1_NodeMouseClick()
        {
            try
            {
                TreeNode tn = new TreeNode();
                tn = e.Node;

                Thread th = new Thread(new ThreadStart(delegate
                    {
                        URLshow(tn);
                    }));
                th.IsBackground = true;
                th.Start();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
        }

        private void URLshow(object tn)
        {
            try
            {
                TreeNode tnn = (TreeNode)tn;
                this.Invoke(new EventHandler(delegate
                    {

                        this.textBox3.Text = FTPIP + tnn.FullPath.ToString().Replace("\\", "/");

                    }));
            }
            catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
        }

猜你喜欢

转载自blog.csdn.net/qq_23833037/article/details/73695022
今日推荐