C#异步平:不是来自Threading.Timer工作 [英] C# Async Ping: not work from a Threading.Timer

查看:190
本文介绍了C#异步平:不是来自Threading.Timer工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能适当地解释。我正在运行每隔X分钟超过6000个并发异步坪的项目。如果我尝试在Windows中运行,从一个按钮组件形成它的作品。但是,如果你从一个Threading.Timer启动有问题。有时候应用程序卡住等待答案坪,有时会引发NullReferenceException异常当您访问我不能为空的变量。但是,如果从一个按钮上的用户申请表工作正常执行。

我希望有人能帮助我。

 使用System.Net.NetworkInformation;
使用的System.Threading;


解决方案

在第一猜我会说,你让你的类中的一些更新GUI。由于该PSS你的任务在GUI线程中运行,可以没有任何问题存在访问一切一切工作的一个按钮$ P $罚款的事实。如果你外包你的任务到它自己的线程,你不必对GUI的直接访问。

要解决这个你可以用图形用户界面调用到(开始)的invoke()调用(一个更深入的文章有关这些命令的差异,可以发现<一个HREF =htt​​p://weblogs.asp.net/justin_rogers/pages/126345.aspx相对=nofollow>这里)。为了使它更容易一些,你也可以使用这些扩展方法之一:

 公共静态类ControlExtensions
{
    公共静态无效InvokeIfRequired(这种控制C,动作&lt;控制&GT;动作)
    {
        如果(c.InvokeRequired)
        {
            c.Invoke(新动作(()=&GT;动作(C)));
        }
        其他
        {
            动作(三);
        }
    }    公共静态无效BeginInvokeIfRequired(这种控制C,动作&lt;控制&GT;动作)
    {
        如果(c.InvokeRequired)
        {
            c.BeginInvoke(新动作(()=&GT;动作(C)));
        }
        其他
        {
            动作(三);
        }
    }
}

的用法是:

  myTextBox.InvokeIfRequired((CTRL)=&GT; ctrl.Text ==SomeNewText);

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;

解决方案

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");

这篇关于C#异步平:不是来自Threading.Timer工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆