如何在C#中将参数传递给线程

本文关键字:参数传递 线程 | 更新日期: 2023-09-27 18:27:26

all我搜索了这个问题,找到了很多答案,不难找到我问题的答案。但是,我有一段奇怪的经历,我不知道为什么我会请人们给我一些建议。这是我的代码:

    void SetThread()
    {
        for (int i = 0; i < _intArrayLength; i++)
        {
            Console.Write(string.Format("SetThread->i: {0}'r'n", i));
            _th[i] = new Thread(new ThreadStart(() => RunThread(i)));
            _th[i].Start();
        }
    }
    void RunThread(int num)
    {
        Console.Write(string.Format("RunThread->num: {0}'r'n", num));
    }

是的,它们是普通的线程代码。我预计所有线程数组都应该调用RunThread方法10次。它应该像

SetThread->i: 0
SetThread->i: 1
SetThread->i: 2
SetThread->i: 3
SetThread->i: 4
SetThread->i: 5
SetThread->i: 6
SetThread->i: 7
SetThread->i: 8
SetThread->i: 9
RunThread->num: 0
RunThread->num: 1
RunThread->num: 2
RunThread->num: 3
RunThread->num: 4
RunThread->num: 5
RunThread->num: 6
RunThread->num: 7
RunThread->num: 8
RunThread->num: 9

这就是我所期望的。顺序并不重要。但我得到的结果如下。

SetThread->i: 0
SetThread->i: 1
SetThread->i: 2
The thread '<No Name>' (0x18e4) has exited with code 0 (0x0).
The thread '<No Name>' (0x11ac) has exited with code 0 (0x0).
The thread '<No Name>' (0x1190) has exited with code 0 (0x0).
The thread '<No Name>' (0x1708) has exited with code 0 (0x0).
The thread '<No Name>' (0xc94) has exited with code 0 (0x0).
The thread '<No Name>' (0xdac) has exited with code 0 (0x0).
The thread '<No Name>' (0x12d8) has exited with code 0 (0x0).
The thread '<No Name>' (0x1574) has exited with code 0 (0x0).
The thread '<No Name>' (0x1138) has exited with code 0 (0x0).
The thread '<No Name>' (0xef0) has exited with code 0 (0x0).
SetThread->i: 3
RunThread->num: 3
RunThread->num: 3
RunThread->num: 3
SetThread->i: 4
RunThread->num: 4
SetThread->i: 5
SetThread->i: 6
RunThread->num: 6
RunThread->num: 6
SetThread->i: 7
RunThread->num: 7
SetThread->i: 8
RunThread->num: 8
SetThread->i: 9
RunThread->num: 9
RunThread->num: 10

我所期望的是RunThread函数应该携带从0到9的参数(num)。我不知道那个错误信息是什么。"线程''~~等等。有人能给我一些线索吗?

如何在C#中将参数传递给线程

您正在循环变量上创建一个闭包-一个简单的解决方案是只创建一个本地副本,因此您的线程使用所需的值:

void SetThread()
    {
        for (int i = 0; i < _intArrayLength; i++)
        {
           int currentValue = i;
            Console.Write(string.Format("SetThread->i: {0}'r'n", i));
            _th[i] = new Thread(() => RunThread(currentValue));
            _th[i].Start();
        }
    }

您可能需要像这样更改代码以使用ParameterizedThreadStart委托:

    for (int i = 0; i < _intArrayLength; i++)
    {
        Console.Write(string.Format("SetThread->i: {0}'r'n", i));
        _th[i] = new Thread((a) => RunThread((int)a));
        _th[i].Start(i);
    }

否则,从线程入口点委托() => RunThread(i),您将从父主线程的上下文访问变量i,这可能会在新线程启动之前发生更改。