Reputation: 148
I was wondering how one would go about showing the status of the internet connecting in WPF C#?
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".
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.
Upvotes: 1
Views: 1090
Reputation: 122
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 :
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;
}
}
Upvotes: -1
Reputation: 3915
You can use 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. You can update UI
only on UI
thread. In order to achieve this you have to call dispatcher to invoke your code on UI
thread, like this:
public void OnNetworkAvailabilityChanged(bool isAvailable)
{
Application.Current.Dispatcher.Invoke(() => {
if (isAvailable)
{
// Change color to available
return;
}
//Change color to unavailable
});
}
Upvotes: 4