Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Lambda表达式向线程传递参数时工作不正常_C#_Multithreading_Lambda_Parameter Passing - Fatal编程技术网

C# 使用Lambda表达式向线程传递参数时工作不正常

C# 使用Lambda表达式向线程传递参数时工作不正常,c#,multithreading,lambda,parameter-passing,C#,Multithreading,Lambda,Parameter Passing,我正在尝试向线程传递多个参数 不过,我只使用一个参数进行测试,并且Lambda表达式没有正确地传递参数的值。ParameterizedThreadStart工作正常,但我只能传递一个对象变量,不能传递更多,这限制了我 我为每种方法都做了一个示例,Lambda表达式方法的输出不正确 对于这两种情况,numofortthreads=2 使用参数化线程启动 这种情况下的输出为: 这是端口0 这是端口1 但是,使用Lambda表达式 public void InitializePorts(int n

我正在尝试向线程传递多个参数

不过,我只使用一个参数进行测试,并且Lambda表达式没有正确地传递参数的值。ParameterizedThreadStart工作正常,但我只能传递一个对象变量,不能传递更多,这限制了我

我为每种方法都做了一个示例,Lambda表达式方法的输出不正确

对于这两种情况,numofortthreads=2

使用参数化线程启动


这种情况下的输出为:

这是端口0
这是端口1


但是,使用Lambda表达式

public void InitializePorts(int numOfPortThreads)
        {
            Thread[] tPortArr = new Thread[numOfPortThreads];

            for (int i = 0; i < numOfPortThreads; i++)
            {

                tPortArr[i] = new Thread( () => new PortSim().PortRun(i));
                tPortArr[i].Start();
            }
        }

这种情况下的输出为:

这是端口2
这是端口2



第二个例子有什么问题?为什么它会产生不正确的结果?

您需要像这样引入局部变量

public void InitializePorts(int numOfPortThreads)
        {
            Thread[] tPortArr = new Thread[numOfPortThreads];

            for (int i = 0; i < numOfPortThreads; i++)
            {
                int j = i;
                tPortArr[j] = new Thread( () => new PortSim().PortRun(j));
                tPortArr[j].Start();
            }
        }
public void initializeport(int numofortthreads)
{
线程[]tPortArr=新线程[numofortthreads];
对于(int i=0;inewportsim().PortRun(j));
tPortArr[j].Start();
}
}

你可能想知道为什么只在c#

中使用谷歌闭包。旁注:如果你不打算让答案比许多重复的答案更好,你应该投票关闭……我还在谷歌上搜索,并在网上发现了这篇文章:。它详细地解释了这一点。谢谢你的回答。我真的需要知道为什么会发生这种情况。我也在这些线索中找到了答案。如果可能的话,请结束这个问题。谢谢问题已经解决了。你可以通过文章底部的“删除”链接自己删除它。
public void InitializePorts(int numOfPortThreads)
        {
            Thread[] tPortArr = new Thread[numOfPortThreads];

            for (int i = 0; i < numOfPortThreads; i++)
            {

                tPortArr[i] = new Thread( () => new PortSim().PortRun(i));
                tPortArr[i].Start();
            }
        }
public void PortRun(int portID)
        {

            portStopWatch.Start();


            Console.WriteLine("This is Port {0}", portID);
            Console.ReadKey();

        }
public void InitializePorts(int numOfPortThreads)
        {
            Thread[] tPortArr = new Thread[numOfPortThreads];

            for (int i = 0; i < numOfPortThreads; i++)
            {
                int j = i;
                tPortArr[j] = new Thread( () => new PortSim().PortRun(j));
                tPortArr[j].Start();
            }
        }