C# 判断设备是否在线
来源:互联网 发布:贵州大数据产业规模 编辑:程序博客网 时间:2024/05/22 10:52
1 ping类
using System;using System.Collections.Generic;using System.Text;using System.Net.NetworkInformation;using System.Threading;using System.Windows.Forms;namespace TestLoadForm{ class PingIp { Thread thread; int ping_time = 2000; System.Timers.Timer timer; public string Ip; public delegate void PingHandle(string ip,bool flag); public event PingHandle pingEvent; public PingIp(string ip) { Ip = ip; thread = new Thread(new ThreadStart(RunSecondThread)); thread.Start(); } void RunSecondThread() { timer = new System.Timers.Timer(ping_time); timer.AutoReset = true; timer.Enabled = true; timer.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed); } void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (PingStop == false) { AutoPingIP(Ip); } else { timer.Stop(); } } bool PingStop = false; void AutoPingIP(string ip) { Ping p = new Ping(); PingOptions ops = new PingOptions(); ops.DontFragment = true; string d = "test data"; byte[] buf = Encoding.ASCII.GetBytes(d); int timeout = 3000; PingReply pr = p.Send(ip, timeout, buf, ops); if(ip != "") { if (pr.Status == IPStatus.Success) { if (pingEvent != null) pingEvent(ip,true); } else { if (pingEvent != null ) { PingStop = true; pingEvent(ip,false); } } } } public void StopTh() { if (timer != null) { timer.Dispose(); } if (thread != null) { if (thread.IsAlive) thread.Abort(); } } }}
2 测试
public partial class Form1 : Form { public Form1() { InitializeComponent(); } PingIp ping; private void Form1_Load(object sender, EventArgs e) { ping = new PingIp("192.168.100.204"); ping.pingEvent += new PingIp.PingHandle(ping_pingEvent); } void ping_pingEvent(string ip, bool flag) { this.Invoke(new MethodInvoker(delegate() { textBox1.Text += "ip:"+ip+" state:"+flag+"\r\n"; } )); } }
3 效果图
1 0
- C# 判断设备是否在线
- 使用RTSP判断摄像机设备是否在线以及快照抓取
- 判断计算机是否在线
- 判断用户是否在线
- 判断QQ是否在线
- ngCordova判断是否在线
- js 判断是否在线
- 判断设备是否越狱
- 判断设备是否越狱
- 判断是否越狱设备
- 判断设备是否越狱
- 判断是否是安卓设备
- 如何判断当前是否在线!
- 判断QQ是否在线.txt
- VB判断QQ是否在线
- JavaScript判断qq是否在线
- html5判断浏览器是否在线
- 判断设备是否插入耳机
- java之TreeMap/TreeSet篇
- 不要和一种编程语言厮守终生:为工作正确选择
- 教你一步一步用OpenGL写游戏——前言
- android-Ultra-Pull-To-Refresh上拉下拉结合通用适配器的使用
- 在Eclipse中使用JUnit4进行单元测试(中级篇)
- C# 判断设备是否在线
- MapReduce的map端join
- Qt学习之路(7): 创建一个对话框(上)
- Navicat Premium连接Oracle数据库报错
- Hbase1.1.3的相关使用心得
- Linux/Windows JDK8安装|JDK1.8安装和环境变量配置
- IOS 系统打电话事件的监听与状态获取
- 统计学习方法-感知机
- 【VISIO】visio画图使用技巧