Detect Changes in Network Connectivity
Some times you will need a mechanism to check whether changes to network occurring during running your application.
So as a solution for this you can add handlers to the static NetworkAddressChanged and NetworkAvailabilityChanged events implemented by theSystem.Net.NetworkInformation.NetworkChange class.
Souce Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Add the handlers to the NetworkChange events.
NetworkChange.NetworkAvailabilityChanged +=
NetworkAvailabilityChanged;
NetworkChange.NetworkAddressChanged +=
NetworkAddressChanged;
Console.ReadLine();
}
// Declare a method to handle NetworkAvailabilityChanged events.
private static void NetworkAvailabilityChanged(
object sender, NetworkAvailabilityEventArgs e)
{
// Report whether the network is now available or unavailable.
if (e.IsAvailable)
{
Console.WriteLine("Network Available");
}
else
{
Console.WriteLine("Network Unavailable");
}
}
// Declare a method to handle NetworkAdressChanged events.
private static void NetworkAddressChanged(object sender, EventArgs e)
{
Console.WriteLine("Current IP Addresses:");
// Iterate through the interfaces and display information.
foreach (NetworkInterface ni in
NetworkInterface.GetAllNetworkInterfaces())
{
foreach (UnicastIPAddressInformation addr
in ni.GetIPProperties().UnicastAddresses)
{
Console.WriteLine(" - {0} (lease expires {1})",
addr.Address, DateTime.Now +
new TimeSpan(0, 0, (int)addr.DhcpLeaseLifetime));
}
}
}
}
}
http://www.codeproject.com/Articles/206720/Simple-Network-Status-Monitor-Example
Detect Changes in Network Connectivity的更多相关文章
- ionic_ Network connectivity error occurred, are you offline?
错误如下: HenHouse admin$ ionic cordova build ios --prod > ionic integrations enable cordova ✖ Downlo ...
- Coursera Algorithms week1 查并集 练习测验:1 Social network connectivity
题目原文描述: Given a social network containing. n members and a log file containing m timestamps at which ...
- [转]Peer-to-Peer Communication Across Network Address Translators
Peer-to-Peer Communication Across Network Address Translators Bryan Ford Massachusetts Institute of ...
- Android开发训练之第五章——Building Apps with Connectivity & the Cloud
Building Apps with Connectivity & the Cloud These classes teach you how to connect your app to t ...
- RabbitMQ Network Partitions
Clustering and Network Partitions RabbitMQ clusters do not tolerate network partitions well. If you ...
- Managing Network Usage
This lesson describes how to write applications that have fine-grained control over their usage of n ...
- Configure a bridged network interface for KVM using RHEL 5.4 or later?
environment Red Hat Enterprise Linux 5.4 or later Red Hat Enterprise Linux 6.0 or later KVM virtual ...
- Simple Network Management Protocol - SNMP Tutorial
30.9 Simple Network Management Protocol Network management protocols specify communication between t ...
- How Network Load Balancing Technology Works--reference
http://technet.microsoft.com/en-us/library/cc756878(v=ws.10).aspx In this section Network Load Balan ...
随机推荐
- 10 tensorflow在循环体中用tf.print输出节点内容
代码 i=tf.constant(0,dtype=tf.int32) batch_len=tf.constant(10,dtype=tf.int32) loop_cond = lambda a,b: ...
- 风险管理,未雨绸缪——《代码之殇》读书笔记II
这次的内容主要是关于软件开发过程中的风险管理,包括项目用时估计.产品的发布与更新.承诺兑现的重要性. ①项目用时估计 有人会质疑项目用时估计的可靠性,因为就事而言,这次的任务可能和上次不一样了,开发环 ...
- 最常见到的runtime exception 异常
ArithmeticException, 算术异常ArrayStoreException, 将数组类型不兼容的值赋值给数组元素时抛出的异常BufferOverflowException, 缓冲区溢出异 ...
- Spring手册
一.Spring 简介 二.结构体系 三.七大主要模块 四.Spring Maven依赖 五 .Sprinf framework 一.Spring 简介 spring是一个开源的轻量级的应用开发框架, ...
- 《SQL Server 2008从入门到精通》--20180710
目录 1.使用Transact-SQL语言编程 1.1.数据定义语言DDL 1.2.数据操纵语言DML 1.3.数据控制语言DCL 1.4.Transact-SQL语言基础 2.运算符 2.1.算数运 ...
- 写markdown用于Github上readme.md文件
Markdown 文档地址 http://pan.baidu.com/s/1o6nu9To Markdown还有一系列衍生版本,用于扩展Markdown的功能(如表格.脚注.内嵌HTML等等),这些功 ...
- 关于UIScrollView有些你很难知晓的崩溃情形
关于UIScrollView有些你很难知晓的崩溃情形 为了实现以下的功能(按钮之间的切换效果): 简短的代码如下: // // RootViewController.m // BUG // // Co ...
- CentOS针对磁盘IO[jdb2进程]的优化
CentOS的jdb2进程总是沾满io,查了一些资料后才知道,这个问题源自系统bug,在此记录一下解决办法: 将高IO的磁盘,用以下参数remount即可 mount -t ext4 -o remou ...
- 铁乐学python_Day42_锁和队列
铁乐学python_Day42_锁和队列 例:多个线程抢占资源的情况 from threading import Thread import time def work(): global n tem ...
- 铁乐学Python_day12_作业
1.写函数,返回一个扑克牌列表,里面有52项,每一项是一个元组 例如:[('红心',2),('草花',2), -('黑桃','A')] def poker(): suit = ['红心', '梅花', ...