[英]How to show the status of an internet connection?
I was wondering how one would go about showing the status of the internet connecting in WPF C#? 我想知道如何显示WPF C#中的Internet连接状态?
What I want to do is that if a connection is available, a circular textbox would show the color green else red. 我想做的是,如果连接可用,则圆形文本框将显示绿色,否则显示红色。 I already have the circular textbox in place.
我已经有了圆形文本框。 I am confused about how the code would be able to keep checking for a connection?
我对代码如何能够继续检查连接感到困惑? Right now it just checks on compile time and that's it.
现在,它只是检查编译时间而已。 I am still trying to learn how all this works, so any suggestion in terms of how this could be done differently would be highly appreciated!
我仍在尝试了解所有方法的工作原理,因此,对如何以不同方式完成此操作的任何建议将不胜感激!
Edit: My code currently looks like this. 编辑:我的代码当前看起来像这样。
public LoginWindow()
{
InitializeComponent();
username.Focus();
NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;
var isAvailable = NetworkInterface.GetIsNetworkAvailable();
OnNetworkAvailabilityChanged(isAvailable);
}
public void OnNetworkAvailabilityChanged(bool isAvailable)
{
if (isAvailable == true)
{
wifiAvailability.Background = Brushes.LightGreen;
}
else
{
wifiAvailability.Background = Brushes.Red;
}
}
public void OnNetworkAvailabilityChanged(object obj, NetworkAvailabilityEventArgs eventArgs)
{
OnNetworkAvailabilityChanged(eventArgs.IsAvailable);
}
Edit: The exception is "System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it". 编辑:异常为“ System.InvalidOperationException:调用线程无法访问此对象,因为其他线程拥有它”。
Depending on whether or not an internet connection is available, the exception happens inside the if-statement of the public void OnNetworkAvailabilityChanged(bool isAvailable)
method. 根据是否可以使用Internet连接,该异常发生在
public void OnNetworkAvailabilityChanged(bool isAvailable)
方法的if语句内。
You can use NetworkChange.NetworkAvailabilityChanged
. 您可以使用
NetworkChange.NetworkAvailabilityChanged
。 It fires everytime network status changes, so there is no need for timers or tasks. 每当网络状态更改时,它就会触发,因此不需要计时器或任务。
NetworkChange.NetworkAvailabilityChanged += (obj, eventArgs) =>
{
if (eventArgs.IsAvailable)
{
// Change color to available
return;
}
//Change color to unavailable
};
If you want to keep it cleaner you can move it to a separate method. 如果要保持清洁,可以将其移至另一种方法。
NetworkChange.NetworkAvailabilityChanged += NetworkAvailabilityChanged;
public void NetworkAvailabilityChanged(object obj, NetworkAvailabilityEventArgs eventArgs) {}
Edit: 编辑:
It won't fire immediately, because it only fires on change. 它不会立即触发,因为它只会在更改时触发。 If you want to immediately check for network you can expand this, for example like this:
如果要立即检查网络,可以扩展此网络,例如:
public YourConstructor()
{
NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;
var isAvailable = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
OnNetworkAvailabilityChanged(isAvailable);
}
public void OnNetworkAvailabilityChanged(bool isAvailable)
{
if (isAvailable)
{
//
}
else
{
//
}
}
public void OnNetworkAvailabilityChanged(object obj, NetworkAvailabilityEventArgs eventArgs)
{
OnNetworkAvailabilityChanged(eventArgs.IsAvailable);
}
Edit2: I don't have this issue, but NetworkChange.NetworkAvailabilityChanged
is called from another thread. Edit2:我没有这个问题,但是
NetworkChange.NetworkAvailabilityChanged
是从另一个线程调用的。 You can update UI
only on UI
thread. 您只能在
UI
线程上更新UI
。 In order to achieve this you have to call dispatcher to invoke your code on UI
thread, like this: 为了实现这一点,您必须调用调度程序以在
UI
线程上调用您的代码,如下所示:
public void OnNetworkAvailabilityChanged(bool isAvailable)
{
Application.Current.Dispatcher.Invoke(() => {
if (isAvailable)
{
// Change color to available
return;
}
//Change color to unavailable
});
}
You can implement this trusted code. 您可以实现此受信任的代码。
If you need it to check redundantly you can call it in a thread that executes every n sec or min : 如果您需要它进行冗余检查,则可以在每n秒或min执行一次的线程中调用它:
private bool CheckConnection(String URL)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Timeout = 5000;
request.Credentials = CredentialCache.DefaultNetworkCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return response.StatusCode == HttpStatusCode.OK;
}
catch
{
return false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.