如何并行迭代动态值字典?

Ale*_*lex 1 c# multithreading parallel.foreach

我试图使用dict中的值副本为每个循环生成线程.

我最初的理解是,这foreach将创造一个新的范围,并导致:

Dictionary<string, string> Dict = new Dictionary<string, string>() { { "sr1", "1" }, { "sr2", "2" } };
foreach (KeyValuePair<string, string> record in Dict) {
    new System.Threading.Timer(_ =>
    {
        Console.WriteLine(record.Value);
    }, null, TimeSpan.Zero, new TimeSpan(0, 0, 5));
}
Run Code Online (Sandbox Code Playgroud)

写道

1
2
2
2
Run Code Online (Sandbox Code Playgroud)

而不是(预期):

1
2
1
2
Run Code Online (Sandbox Code Playgroud)

所以我尝试在foreach中克隆kvp:

KeyValuePair<string, string> tmp = new KeyValuePair<string, string>(record.Key, record.Value);
Run Code Online (Sandbox Code Playgroud)

但这会产生相同的结果.

我也试过了,System.Parallel.ForEach但似乎需要非动态的价值,这对我的字典来说有点火车粉碎.

如何用线程迭代我的字典?

Sco*_*ain 7

问题是关闭lambda,修复的方法是在for循环中添加一个局部变量

Dictionary<string, string> Dict = new Dictionary<string, string>() { { "sr1", "1" }, { "sr2", "2" } };
foreach (KeyValuePair<string, string> record in Dict) {

    var localRecord = record;
    new System.Threading.Timer(_ =>
    {
        Console.WriteLine(localRecord.Value);
    }, null, TimeSpan.Zero, new TimeSpan(0, 0, 5));
}
Run Code Online (Sandbox Code Playgroud)

您的版本中发生的是它捕获变量record而不是变量记录的.因此,当计时器第二次运行时,它使用的"当前值" record是数组中的第二个元素.

在幕后,这就是您的代码版本中发生的事情.

public void MainFunc()
{
    Dictionary<string, string> Dict = new Dictionary<string, string>() { { "sr1", "1" }, { "sr2", "2" } };
    foreach (KeyValuePair<string, string> record in Dict) {

        _recordStored = record;
        new System.Threading.Timer(AnnonFunc, null, TimeSpan.Zero, new TimeSpan(0, 0, 5));
    }
}

private KeyValuePair<string, string> _recordStored;

private void AnnonFunc()
{
    Console.WriteLine(_recordStored.Value);
}
Run Code Online (Sandbox Code Playgroud)

看看你的函数在第一次运行时如何运行它的正确版本_recordStored,但是在_recordStored被覆盖后它将只显示最后一个设置值.通过创建局部变量,它不会覆盖.

一种想象它的方法(我不确定如何在代码示例中表示它)是它创建_recordStored1第一个循环,_recordStored2第二个循环,依此类推.该函数_recordStored#在调用函数时使用for 的正确版本.