C# 异步 Ping:无法通过 Threading.Timer 工作

发布于 2024-10-11 09:04:57 字数 323 浏览 3 评论 0原文

我希望能够正确解释。我正在开发一个每 X 分钟运行超过 6000 个同步异步 ping 的项目。如果我尝试从 Windows 窗体上的按钮运行该组件,它就会起作用。但如果你从“Threading.Timer”开始,就会出现问题。有时应用程序会卡住等待 ping 的答案,有时当您访问 I 不能为 null 的变量时会抛出 NullReferenceException 异常。但如果从用户请求表单上的按钮执行,则效果很好。

我希望有人能帮助我。

using System.Net.NetworkInformation;
using System.Threading;

I hope to explain properly. I'm working on a project that runs more than 6000 simultaneous asynchronous pings every X minutes. If I try to run the component from a button on a windows form it works. But if you start from a "Threading.Timer" it has problems. Sometimes the application gets stuck waiting for answers to pings, and sometimes throws NullReferenceException exception when you access a variable that I can not be null. But if executed from a button on a user request form work fine.

I hope someone can help me.

using System.Net.NetworkInformation;
using System.Threading;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九歌凝 2024-10-18 09:04:57

初步猜测,我会说您对班级中的 GUI 进行了一些更新。由于按按钮一切工作正常,因此您的任务在 GUI 线程中运行,并且可以毫无问题地访问那里的所有内容。如果您将任务外包到它自己的线程中,您将无法直接访问 GUI。

要解决此问题,您可以将 gui 调用包装到 (Begin)Invoke() 调用中(有关这些命令差异的更深入的文章可以在 此处)。为了使它更容易一点,您还可以使用以下扩展方法之一:

public static class ControlExtensions
{
    public static void InvokeIfRequired(this Control c, Action<Control> action)
    {
        if (c.InvokeRequired)
        {
            c.Invoke(new Action(() => action(c)));
        }
        else
        {
            action(c);
        }
    }

    public static void BeginInvokeIfRequired(this Control c, Action<Control> action)
    {
        if (c.InvokeRequired)
        {
            c.BeginInvoke(new Action(() => action(c)));
        }
        else
        {
            action(c);
        }
    }
}

用法是:

myTextBox.InvokeIfRequired((ctrl) => ctrl.Text == "SomeNewText");

At a first guess i would say you make some updates to the GUI within your class. Due to the fact that everything works fine on a button press your task runs within the GUI thread and can access everything there without any problems. If you outsource your task into its own thread you don't have direct access to the GUI.

To resolve this you can wrap the gui calls into an (Begin)Invoke() call (a more in-depth article about these commands differences can be found here). To make it a little easier you can also use one of these extension methods:

public static class ControlExtensions
{
    public static void InvokeIfRequired(this Control c, Action<Control> action)
    {
        if (c.InvokeRequired)
        {
            c.Invoke(new Action(() => action(c)));
        }
        else
        {
            action(c);
        }
    }

    public static void BeginInvokeIfRequired(this Control c, Action<Control> action)
    {
        if (c.InvokeRequired)
        {
            c.BeginInvoke(new Action(() => action(c)));
        }
        else
        {
            action(c);
        }
    }
}

The usage would be:

myTextBox.InvokeIfRequired((ctrl) => ctrl.Text == "SomeNewText");
梦年海沫深 2024-10-18 09:04:57

您是否尝试使用多个BackgroundWorker而不是计时器?

http://msdn.microsoft.com/en-us/library /system.componentmodel.backgroundworker.aspx

Did you try using several BackgroundWorker instead of a Timer?

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

寒江雪… 2024-10-18 09:04:57

感谢大家的帮助!我找到了解决方案(针对工作时间)。我所做的只是改变你销毁“ping”对象以接收重播的方式。现在使用参数“sender”(引用 ping)销毁方法“PingResult”中的每个对象“ping”,如下所示:

((Ping)sender).PingCompleted -= PingResult;
((IDisposable)sender).Dispose();

这对我有用。我希望它对其他人有帮助。

问候并非常感谢您所做的一切!

Thank all for your help! I found the solution (for the time worked). All I did was change the way you destroy the objects "ping" to have received your replay. Now destroy each object "ping" in the method "PingResult" using the parameter "sender" (reference to the ping) as follows:

((Ping)sender).PingCompleted -= PingResult;
((IDisposable)sender).Dispose();

This works for me. I hope it helps others.

A greeting and thank you very much for everything!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文