gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:10:13 26 4
gpt4 key购买 nike

我正在尝试编写一个不断在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com