-
Notifications
You must be signed in to change notification settings - Fork 0
InternetConnectionHelper
The InternetConnectionHelper
static class allows you to monitor Internet connection availability. Let's see an example:
using MochaCore.Utils;
InternetConnectionHelper.InternetAvailabilityChanged += InternetAvailabilityChanged;
private void InternetAvailabilityChanged(object? sender, InternetConnectionAvailabilityChangedEventArgs e)
{
if (e.IsInternetConnectionAvailable)
{
Console.WriteLine("You have Internet connection!");
}
else
{
Console.WriteLine("No connection to Internet!");
}
}
Caution
Be aware that InternetAvailabilityChanged
handler will be invoked on different threads. If you intend to modify UI from the handler code use main thread dispatcher.
The InternetConnectionHelper
maintains a list of IP addresses that are pinged one by one periodically at a constant interval (default is 4 seconds). If any of the addresses fails to respond, all addresses are pinged simultaneously. If at least one of them responds successfully, the Internet connection is treated as available; otherwise, the InternetConnectionHelper
notifies you of a lack of Internet connection. If the Internet connection is confirmed to be available after pinging all addresses at once, the address that initially failed will be by default blocked for 3 minutes.
No addresses will be pinged if there are no subscribers to the InternetAvailabilityChanged
event.
You can manually check the Internet connection at any time by invoking the CheckInternetAvailabilityAsync()
method. If the result of this method differs from the last stored value, the InternetAvailabilityChanged
event will be triggered.
Similarly, you can use the IsInternetAvailable
property to synchronously retrieve the last stored result of the Internet availability check.
The InternetConnectionAvailabilityChangedEventArgs
class includes a IsConnectionWireless
read-only property. This property can return the following values:
-
true
- The network interface responsible for the Internet connection is wireless. -
false
- The network interface responsible for the Internet connection is not wireless. -
null
- The network interface responsible for the Internet connection could not be determined.
You can check this value at any time using the HasWirelessConnection()
method.
The InternetConnectionHelper
supports customization via the InternetConnectionHelper.Configure
object. You can modify the following settings:
-
CheckInterval
- Specifies the time interval between consecutive pings. -
BlockingPeriod
- Specifies the amount of time the faulted IP address is blocked -
DiagnosticAddresses
- A list of IP addresses to ping periodically. -
PingBuilder
- Defines how Ping instances are created.
The InternetConnectionHelper
is thread-safe, meaning it can be safely used concurrently by multiple threads. All critical operations—such as starting/stopping the monitoring loop, performing availability checks, and modifying configurations—can be safely invoked at any time.
The InternetAvailabilityChanged
event handlers are executed on different threads. Unhandled exceptions occurring within these handlers will terminate the invoking thread and are not propagated elsewhere. As a result, you might overlook issues where a handler throws an unexpected exception, such as a NullReferenceException
. Additionally, if the first handler in the invocation list throws an exception, subsequent listeners will not receive notifications either.
To mitigate this behavior, the InternetConnectionHelper.Configure
object includes a RethrowHandlersExceptionsIntoSynchronizationContext()
method. If your application uses a synchronization context (e.g., a GUI application), you can provide this context to the InternetConnectionHelper
. The InternetConnectionHelper
will then propagate exceptions to the synchronization context, making them easier to notice or debug.
// while being on the UI thread
InternetConnectionHelper.Configuration.RethrowHandlersExceptionsIntoSynchronizationContext(SynchronizationContext.Current);
The InternetConnectionHelper
logs critical operations using Debug.WriteLine()
. These logs can be viewed in the Output tab of Visual Studio for debugging purposes.
InternetConnectionHelper
is a static class, meaning modifying its configuration will impact all subscribers globally