c# - 在控制台中工作但在 Windows 窗体中不工作的异步代码

标签 c# winforms multithreading asynchronous

我正在尝试编写一个不断在 LAN 上搜索主机的应用程序。当我将其作为控制台运行时,因为 countdown.Wait() 似乎工作正常。但是,当我将代码放入 Windows 窗体时,countdown.Signal() 似乎并没有减少其计数器。不确定是什么问题。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Diagnostics;
using System.Net;
using System.Threading;

namespace Multi_Threaded
{
    public partial class Form1 : Form
    {

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        PortScanner ps = new PortScanner();
        ps.ProbeCompleted += new PingProbeCompleted(ps_ProbeCompleted);

        ps.run_ping_probe();
    }

    void ps_ProbeCompleted(object sender, PingProbeCompletedArguments e)
    {
        MessageBox.Show("I found " + e.ip_adresses_list_of_host.Count.ToString() + "host(s)");
    }
}

public delegate void PingProbeCompleted(object sender,PingProbeCompletedArguments e);
public class PingProbeCompletedArguments : EventArgs
{
    public List<string> ip_adresses_list_of_host;
}
public class PortScanner
{
    public event PingProbeCompleted ProbeCompleted;
    static List<string> ip_adresses = new List<string>();

    static CountdownEvent countdown;

    public void run_ping_probe()
    {
        ip_adresses.Clear();

        countdown = new CountdownEvent(1);

        string ipBase = "10.125.";
        for (int sub = 0; sub < 14; sub++)
        {
            for (int i = 1; i < 255; i++)
            {
                string ip = ipBase + sub.ToString() + "." + i.ToString();
                Ping p = new Ping();
                p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
                countdown.AddCount();
                p.SendAsync(ip, 100, ip);
            }
        }
        countdown.Signal();
        countdown.Wait();
        PingProbeCompletedArguments e = new PingProbeCompletedArguments();
        e.ip_adresses_list_of_host = ip_adresses;
        ProbeCompleted(this, e);

    }

    private void p_PingCompleted(object sender, PingCompletedEventArgs e)
    {
        string ip = (string)e.UserState;
        if (e.Reply.Status == IPStatus.Success)
        {
            ip_adresses.Add(ip + "\t" + e.Reply.RoundtripTime + " ms");
        }
        countdown.Signal();
    }
}

最佳答案

是的,当您在 Winforms 项目中使用它时,您的代码会死锁。问题在于 Ping 类尽最大努力在调用 SendAsync() 的同一线程上引发 PingCompleted 事件。它使用 AsyncOperationManager.CreateOperation() 方法来执行此操作。

问题是,这实际上适用于 Winforms 应用程序。它尝试在主线程上引发事件。但这行不通,因为您使用 countdown.Wait() 调用阻塞了主线程。由于主线程被阻塞,ping 无法完成。主线程无法完成,因为 ping 未完成。死锁城。

它在控制台模式应用程序中工作,因为它没有像 Winforms 那样的同步提供程序。 PingComplete 事件将在线程池线程上引发。

阻塞 UI 线程从根本上是有缺陷的。快速修复是在工作线程上运行代码。请注意,这也会在该工作人员上触发 ProbeCompleted 事件。使用 Control.BeginInvoke() 将其编码到 UI 线程。或者使用 BackgroundWorker。

    private void Form1_Load(object sender, EventArgs e) {
        PortScanner ps = new PortScanner();
        ps.ProbeCompleted += new PingProbeCompleted(ps_ProbeCompleted);
        ThreadPool.QueueUserWorkItem((w) => ps.run_ping_probe());
    }

并且不要忘记删除额外的 Signal() 调用。

关于c# - 在控制台中工作但在 Windows 窗体中不工作的异步代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7766953/

相关文章:

C# 如何正确使用CoreWebView2PrintSettings?

c# - 将 Windows 主题更改为高对比度时,将按钮颜色更改为白色

c# - 如何通过迭代行从 DataTable 中检索数据

c - 如何从另一个线程重新启动 posix 线程(在我的例子中是监视线程)

c - C中的原子变量

c# - var、dynamic 和 linq 组合的奇怪行为变化

c# - C# 在图像中绘制一个点

c# - 在 C# 中缩放窗口窗体

c# - 当控件是数据绑定(bind)时,无法以编程方式将行添加到 datagridview 的行集合

java - 同步工作线程使用的共享对象