首页 > 解决方案 > 并行处理列表中可用的项目使用 Parallel.ForEach

问题描述

我正在尝试为我的项目开发网络监视器。我正在使用 Parallel.ForEach 将 ping 并行发送到 List IPListAll 中列出的所有 IP Addressess。

我想处理从所有 PC 收到的回复,但是我的代码永远不会到达 MessageBox.Show 和 DisplayChangeOnReply 函数。我被困在这里。请帮忙,我不知道我做错了什么。我的代码附在下面以供参考。

while(true)
{
    object pinglock = new object();
    Parallel.ForEach(IPListAll,ipaddr =>
    {
        lock(pinglock)
        {
            reply = Ping.Send(ipaddr,500);    // this line is being executed always
            MessageBox.Show(ipaddr);        // Execution not reaching here
        }
        DisplayChangeOnReply(reply,ipaddr); // Execution nor reaching here
    });
}

标签: c#

解决方案


推荐阅读