using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Media;namespace 监视网络连接
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SoundPlayer MyPlayer = new SoundPlayer();
private Microsoft.VisualBasic.Devices.ServerComputer My = new Microsoft.VisualBasic.Devices.ServerComputer();
private void Form1_Load(object sender, EventArgs e)
{
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(AvailabilityChangedCallback);
MyPlayer.SoundLocation = "BEEP2.WAV";
MyPlayer.LoadAsync();
MyPlayer.Play();
} static void AvailabilityChangedCallback(object sender, EventArgs e)
{
SoundPlayer MyPlayer = new SoundPlayer();
Microsoft.VisualBasic.Devices.ServerComputer My = new Microsoft.VisualBasic.Devices.ServerComputer();
if (!My.Network.IsAvailable)
{
MessageBox.Show("网络连接失败!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
MyPlayer.SoundLocation = "ALARM8.WAV";
MyPlayer.LoadAsync();
MyPlayer.Play();
}
else
{
MessageBox.Show("网络连接成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
MyPlayer.SoundLocation = "BASE.WAV";
MyPlayer.LoadAsync();
MyPlayer.Play();
}
}
}
}上面代码执行后,把掉网线,托盘区显示网络断开,但是该程序没有反映。在AvailabilityChangedCallback函数里面设置断点,没有作用,证明没有执行该函数。想问这是为什么?还有,如果在该窗体上有文本框,当网络状态改变时要在文本框里面显示提示信息,但是现在AvailabilityChangedCallback函数里面又不能直接调用文本框对象,那又要怎样修改?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Media;namespace 监视网络连接
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SoundPlayer MyPlayer = new SoundPlayer();
private Microsoft.VisualBasic.Devices.ServerComputer My = new Microsoft.VisualBasic.Devices.ServerComputer();
private void Form1_Load(object sender, EventArgs e)
{
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(AvailabilityChangedCallback);
MyPlayer.SoundLocation = "BEEP2.WAV";
MyPlayer.LoadAsync();
MyPlayer.Play();
} static void AvailabilityChangedCallback(object sender, EventArgs e)
{
SoundPlayer MyPlayer = new SoundPlayer();
Microsoft.VisualBasic.Devices.ServerComputer My = new Microsoft.VisualBasic.Devices.ServerComputer();
if (!My.Network.IsAvailable)
{
MessageBox.Show("网络连接失败!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
MyPlayer.SoundLocation = "ALARM8.WAV";
MyPlayer.LoadAsync();
MyPlayer.Play();
}
else
{
MessageBox.Show("网络连接成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
MyPlayer.SoundLocation = "BASE.WAV";
MyPlayer.LoadAsync();
MyPlayer.Play();
}
}
}
}上面代码执行后,把掉网线,托盘区显示网络断开,但是该程序没有反映。在AvailabilityChangedCallback函数里面设置断点,没有作用,证明没有执行该函数。想问这是为什么?还有,如果在该窗体上有文本框,当网络状态改变时要在文本框里面显示提示信息,但是现在AvailabilityChangedCallback函数里面又不能直接调用文本框对象,那又要怎样修改?
的static去掉就能了而且可以实现改变文本框中的文字代码如下: public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Microsoft.VisualBasic.Devices.ServerComputer My = new Microsoft.VisualBasic.Devices.ServerComputer();
delegate void SetTextCallback(string text);
private void Form1_Load(object sender, EventArgs e)
{
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(AvailabilityChangedCallback);
}
private void SetText(string text)
{
if (textBox1.InvokeRequired)
{
SetTextCallback setTextProc = new SetTextCallback(SetText);
Invoke(setTextProc, new object[] { text });
}
else
{
textBox1.Text = text;
} } void AvailabilityChangedCallback(object sender, EventArgs e)
{
if (!My.Network.IsAvailable)
{
SetText(@"网络连接失败!");
}
else
{
SetText(@"网络连接成功!");
}
}
}
textBox1.Text = @"网络连接失败!";
但是这样做不是线程安全的