我有一个程序可以确定设备是否连接到互联网,当我启动程序时,标签会告诉设备是否已连接,但是当我断开设备连接或将设备连接到互联网时,标签不会更新,并且我需要重新启动应用程序才能看到标签更改。
这是我的代码
public static bool conexion()
{
while (true)
{
try
{
using (var cliente = new WebClient())
using (cliente.OpenRead("http://google.com"))
return true;
}
catch
{
return false;
}
}
}
public MainWindow()
{
InitializeComponent();
if (conexion() == true)
{
label.Content = "Esta conectado";
label.Foreground = new SolidColorBrush(Colors.Green);
}
else if(conexion() == false)
{
label.Content = "No esta conectado";
label.Foreground = new SolidColorBrush(Colors.Red);
}