Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use the main thread for networking #10697

Merged
merged 1 commit into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 15 additions & 36 deletions src/Essentials/src/Connectivity/Connectivity.uwp.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.Maui.ApplicationModel;
using Windows.Networking.Connectivity;

namespace Microsoft.Maui.Networking
Expand All @@ -21,10 +21,18 @@ public NetworkAccess NetworkAccess
{
get
{
return
MainThread.IsMainThread ?
GetNetworkAccess() :
MainThread.InvokeOnMainThreadAsync(GetNetworkAccess).GetAwaiter().GetResult();
var profile = NetworkInformation.GetInternetConnectionProfile();
if (profile == null)
return NetworkAccess.Unknown;

var level = profile.GetNetworkConnectivityLevel();
return level switch
{
NetworkConnectivityLevel.LocalAccess => NetworkAccess.Local,
NetworkConnectivityLevel.InternetAccess => NetworkAccess.Internet,
NetworkConnectivityLevel.ConstrainedInternetAccess => NetworkAccess.ConstrainedInternet,
_ => NetworkAccess.None,
};
}
}

Expand Down Expand Up @@ -62,7 +70,7 @@ public IEnumerable<ConnectionProfile> ConnectionProfiles
continue;
}
}
catch (global::System.Exception ex)
catch (Exception ex)
{
Debug.WriteLine($"Unable to get Network Adapter, returning Unknown: {ex.Message}");
}
Expand All @@ -71,34 +79,5 @@ public IEnumerable<ConnectionProfile> ConnectionProfiles
}
}
}

NetworkAccess GetNetworkAccess()
{
try
{
var profile = NetworkInformation.GetInternetConnectionProfile();

if (profile == null)
return NetworkAccess.Unknown;

var level = profile.GetNetworkConnectivityLevel();

switch (level)
{
case NetworkConnectivityLevel.LocalAccess:
return NetworkAccess.Local;
case NetworkConnectivityLevel.InternetAccess:
return NetworkAccess.Internet;
case NetworkConnectivityLevel.ConstrainedInternetAccess:
return NetworkAccess.ConstrainedInternet;
default:
return NetworkAccess.None;
}
}
catch
{
return NetworkAccess.Unknown;
}
}
}
}
}
11 changes: 11 additions & 0 deletions src/Essentials/test/DeviceTests/Tests/Connectivity_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.Networking;
using Xunit;

Expand All @@ -21,5 +22,15 @@ public void Distict_Connection_Profiles()
var profiles = Connectivity.ConnectionProfiles;
Assert.Equal(profiles.Count(), profiles.Distinct().Count());
}

[Fact]
public async Task Test()
{
var current = Connectivity.Current.NetworkAccess;

var thread = await Task.Run(() => Connectivity.Current.NetworkAccess);

Assert.Equal(current, thread);
}
}
}