如何将变量从函数传输到线程?

问题描述 投票:0回答:1

由于某种原因,这段代码给我带来了一个错误

        private void button1_Click(object sender, EventArgs e)
        {
            treeView1.BeginUpdate();
            TreeNode labelnode = treeView1.Nodes[0].Nodes.Add("Label" + labelnum.ToString());
            treeView1.EndUpdate();

            //create id
            string id = "1";

            Thread thread = new Thread(new ThreadStart(Worker(labelnode, id))); // it errors out here
            thread.Start();
        }

        private void Worker(TreeNode labelnode, string id)
        { 
            while (true)
            {
                Thread.Sleep(50);
                if (labelnode.IsSelected == true)
                {
                    showProperties("label", id);
                }
            }
        }

我在这里尝试做的是选择

labelnode
时显示属性。如果有人帮助我,我会很高兴和感激

c# multithreading winforms
1个回答
0
投票
        Thread thread = new Thread(() => Worker(labelnode, id));
        thread.Start();
© www.soinside.com 2019 - 2024. All rights reserved.