| Component | Build Status | NuGet Package |
|---|---|---|
| System.Net |
NetworkHelper provides two patterns for establishing a network connection: a blocking token-based approach for simple use-cases, and an event-based approach for background connection management.
Call SetupAndConnectNetwork with a CancellationToken timeout. This method can be called repeatedly β if the first attempt times out, call it again:
bool connected = false;
while (!connected)
{
CancellationTokenSource cs = new(30000);
connected = NetworkHelper.SetupAndConnectNetwork(requiresDateTime: true, token: cs.Token);
if (!connected)
{
Debug.WriteLine($"Network not ready, status: {NetworkHelper.Status}");
// wait before retrying
Thread.Sleep(5000);
}
}Call SetupNetworkHelper once at startup. The helper connects in the background. Wait on NetworkReady:
NetworkHelper.SetupNetworkHelper(requiresDateTime: true);
if (!NetworkHelper.NetworkReady.WaitOne(30000, true))
{
Debug.WriteLine($"Failed to connect: {NetworkHelper.Status}");
}Note:
NetworkReadyis reset when the connection is lost and re-signaled when it is restored, accurately reflecting live network state. Code that previously assumedNetworkReadywould remain set after first connect should be updated to handle transient disconnects.
Call Reset() to fully reset the helper so it can be called again with different settings, or to restart after an error:
NetworkHelper.Reset();
// Now call SetupNetworkHelper or SetupAndConnectNetwork again
NetworkHelper.SetupNetworkHelper(requiresDateTime: true);SetupNetworkHelper throws InvalidOperationException if called a second time without a prior Reset(). Token-based methods (SetupAndConnectNetwork) do not have this restriction and are always retryable.
For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.
Join our Discord community here.
The list of contributors to this project can be found at CONTRIBUTORS.
The nanoFramework Class Libraries are licensed under the MIT license.
This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. For more information see the .NET Foundation Code of Conduct.
This project is supported by the .NET Foundation.
