确定是否互联网连接可用 [英] Determine if internet connection is available

查看:210
本文介绍了确定是否互联网连接可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我不是第一个提出这样一个问题:如何才能知道如果我的应用是在线与否? 我发现这个职位:<一href="http://stackoverflow.com/questions/507855/whats-the-easiest-way-to-verify-theres-an-available-network-connection">StackOverflow. 我想用C#和.NET 3.5做到这一点。

I know that I am not the first to ask the question: How do I find out if my application is online or not? I found this post: StackOverflow. I want to do it with C# and .NET 3.5.

的建议是定期ping通的资源。我不是很满意,他的意见。我宁愿检测网络的变化,然后ping我的服务来检查它是否在线。

The recommendation is to ping the resource regularly. I am not very happy with that advice. I would rather detect a network change and THEN ping my service to check if it is online.

.NET提供了两个事件用于此目的: NetworkChange.NetworkAvailabilityChanged NetworkChange.NetworkAddressChanged

.NET provides two events for this purpose: NetworkChange.NetworkAvailabilityChanged NetworkChange.NetworkAddressChanged

第一个事件听起来不错,但是它发射只有在最后的网卡​​是网络下线。我已经安装了由VMware,而这些都是永远在线的多个虚拟网卡。 第二事件工作,但堵塞网络电缆和事件之间,常常出现5秒等待时间。 在Windows任务栏图标或多或少立即做出反应时,我拔出电缆。 什么是要以最快的速度最好的办法,因为这托盘图标?

The first event sounds good but it is fired only if the last network card which is online goes offline. I have several virtual network cards which have been installed by VMWare and those are always online. The second event works but between plugging the network cable and the event, there are often 5 seconds wait time. The Windows tray icon reacts more or less immediately when I am unplugging the cable. What is the best way to be as fast as this tray icon?

我的解决方法是将轮询 NetworkInterface.GetAllNetworkInterfaces() 每500毫秒,并把我自己的事件的情况下,一个网络适配器的状态变化。

My workaround would be to poll NetworkInterface.GetAllNetworkInterfaces() every 500ms and to throw my own event in case that the status of a network adapter changed.

必须有一个更好的解决办法:)

There must be a better solution :)

推荐答案

我试过Webleeuw建议的链接,而是code也可以4到10秒之间需要我的时候插入或拔出我的电缆来通知我。 现在,我想知道,如果它只是我的计算机或安装和我写的是基于NetworkInterface.GetAllNetworkInterfaces我自己的观察类的()。

I tried the link the Webleeuw suggested, but that code also needs between 4 and 10 seconds to notify me when I plug or unplug my cable. Now, I wanted to know if it just my computer or installation and I wrote my own Observer class which is based on NetworkInterface.GetAllNetworkInterfaces().

和:它可以用闪电般的速度。现在我的应用程序反应迅速的做盘。 在code是远从生产code,它仅仅是一个快速的黑客。但是,这是什么,我会建立在现在的:)

And: It works with lightning speed. My app reacts now as quickly as does the tray. The code is far from production code, it is just a quick hack. But this is what I will build upon now :)

using System;
using System.Net.NetworkInformation;
using Timer=System.Threading.Timer;

namespace NetworkCheckApp
{
public class NetworkStatusObserver
{
    public event EventHandler<EventArgs> NetworkChanged;

    private NetworkInterface[] oldInterfaces;
    private Timer timer;

    public void Start()
    {
        timer = new Timer(UpdateNetworkStatus, null, new TimeSpan(0, 0, 0, 0, 500), new TimeSpan(0, 0, 0, 0, 500));

        oldInterfaces = NetworkInterface.GetAllNetworkInterfaces();
    }

    private void UpdateNetworkStatus(object o)
    {
        var newInterfaces = NetworkInterface.GetAllNetworkInterfaces();
        bool hasChanges = false;
        if (newInterfaces.Length != oldInterfaces.Length)
        {
            hasChanges = true;
        }
        if (!hasChanges)
        {
            for (int i = 0; i < oldInterfaces.Length; i++)
            {
                if (oldInterfaces[i].Name != newInterfaces[i].Name || oldInterfaces[i].OperationalStatus != newInterfaces[i].OperationalStatus)
                {
                    hasChanges = true;
                    break;
                }
            }
        }

        oldInterfaces = newInterfaces;

        if (hasChanges)
        {
            RaiseNetworkChanged();
        }
    }

    private void RaiseNetworkChanged()
    {
        if (NetworkChanged != null)
        {
            NetworkChanged.Invoke(this, null);
        }
    }
}
}

这篇关于确定是否互联网连接可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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