使用状态消息运行Ping.SendAsync

时间:2017-07-17 18:05:21

标签: c# .net entity-framework ping

我正在创建一个报告系统来检查刀片的状态。我知道下面的代码可行,但它的速度非常慢。我查找了有关运行Ping.SendAsync的信息,看起来很有希望,但它不会在ping上运行状态报告,除非它在处理程序中。问题是,我需要访问函数中的其他对象才能正确更改网页。

protected void RadGridDellBlade_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
        {
            thing1 t1 = new thing1();
            BladeRunnerDataAccess td = new BladeRunnerDataAccess(t1);
            try
            {
                Image icon = (Image)e.Item.FindControl("Dell_imgIcon");
                BladeWorkstation blade = (BladeWorkstation)e.Item.DataItem;
                Ping pingSender = new Ping();
                PingReply reply = pingSender.Send(blade.IPAddress);

                switch (reply.Status)
                {
                    case IPStatus.Success:
                        icon.ImageUrl = "~/Images/GreenIcon.png";
                        break;
                    case IPStatus.TimedOut:
                        icon.ImageUrl = "~/Images/RedIcon.png";
                        break;
                    default:
                        icon.ImageUrl = "~/Images/GrayIcon.png";
                        break;
                }
                Image Dell_osbit = (Image)e.Item.FindControl("Dell_OSbit");
                switch (blade.BladeOSID)
                {
                    case 1:
                        Dell_osbit.ImageUrl = "~/Images/xp.png";
                        break;
                    case 2:
                        Dell_osbit.ImageUrl = "~/Images/32bit.png";
                        break;
                    case 3:
                        Dell_osbit.ImageUrl = "~/Images/64bit.png";
                        break;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                t1.Dispose();
            }
        }
    }

此代码可以正常运行,但一次只能运行250个刀片,需要几分钟。有没有办法并行或使用线程,并保持状态检查?我感谢任何帮助!!

1 个答案:

答案 0 :(得分:0)

缺乏显示您尝试过的好Minimal, Complete, and Verifiable code example,我无法保证完整而准确的解决方案。但是,您当然可以将较早的基于事件的Ping.SendAsync()模型与async / await结合使用,并将您的代码转换为相当不错的异步实现。

我从您的方法名称中收集您正在执行此工作,因为每个项目都绑定到容器控件。所以你没有向我们展示完成所有250项的实际循环。我假设以异步方式运行事件处理程序的每次调用就足够了。为此:

protected async void RadGridDellBlade_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
    {
        thing1 t1 = new thing1();
        BladeRunnerDataAccess td = new BladeRunnerDataAccess(t1);
        try
        {
            Image icon = (Image)e.Item.FindControl("Dell_imgIcon");
            BladeWorkstation blade = (BladeWorkstation)e.Item.DataItem;
            Ping pingSender = new Ping();
            TaskCompletionSource<PingReply> tcs = new TaskCompletionSource<PingReply>();

            pingSender.PingCompleted += (s, e2) => tcs.SetResult(e2.Reply);
            pingSender.SendAsync(blade.IPAddress, null);
            PingReply reply = await tcs.Task;

            switch (reply.Status)
            {
                case IPStatus.Success:
                    icon.ImageUrl = "~/Images/GreenIcon.png";
                    break;
                case IPStatus.TimedOut:
                    icon.ImageUrl = "~/Images/RedIcon.png";
                    break;
                default:
                    icon.ImageUrl = "~/Images/GrayIcon.png";
                    break;
            }
            Image Dell_osbit = (Image)e.Item.FindControl("Dell_OSbit");
            switch (blade.BladeOSID)
            {
                case 1:
                    Dell_osbit.ImageUrl = "~/Images/xp.png";
                    break;
                case 2:
                    Dell_osbit.ImageUrl = "~/Images/32bit.png";
                    break;
                case 3:
                    Dell_osbit.ImageUrl = "~/Images/64bit.png";
                    break;
            }
        }
        catch (Exception ex)
        {

        }
        finally
        {
            t1.Dispose();
        }
    }
}

对于引发事件并调用上述处理程序的对象,处理程序将在您到达await tcs.Task;语句后立即完成其工作(假设ping尚未完成)。 ping完成后,将继续执行该方法,并使用PingReply的有效值。

当然,在ping完成之前,集合中的每个项目都将具有它开始的任何默认状态。据推测这是可以接受的。如果没有,您将必须创建一些中间状态以在ping正在进行时显示。