-
Notifications
You must be signed in to change notification settings - Fork 328
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
NetworkInformation.GetInternetConnectionProfile()
throws on background threads
#2965
Comments
@mattleibow this doesn't repro with the minimal steps provided. For example, this works:
private async void myButton_Click(object sender, RoutedEventArgs e)
{
myButton.Content = "Clicked";
var profile = await Task.Run(() =>
{
return NetworkInformation.GetInternetConnectionProfile().GetNetworkConnectivityLevel();
});
myButton.Content = profile;
} The following code raises an exception when calling GetNetworkConnectivityLevel: private async void myButton_Click(object sender, RoutedEventArgs e)
{
myButton.Content = "Clicked";
var profile = await Task.Run(() =>
{
return NetworkInformation.GetInternetConnectionProfile();
});
myButton.Content = profile.GetNetworkConnectivityLevel();
} Also, in the following p1 is correctly set but p2 is null private async void myButton_Click(object sender, RoutedEventArgs e)
{
myButton.Content = "Clicked";
var p1 = NetworkInformation.GetInternetConnectionProfile();
var profile = await Task.Run(() =>
{
var p2 = NetworkInformation.GetInternetConnectionProfile();
return p2;
});
myButton.Content = profile.GetNetworkConnectivityLevel();
} |
I don't repro this issue. Is there anything specific about how you've created the thread? I tried this:
There is no exception and the debug output looks good. I tried this on Win11 and Win10 21H1. Note that NetworkInformation is a Windows API and not part of WinAppSDK, so any issue we find here would like require an OS update. |
I believe the thread was created using the |
I also think the issue is that the profile was obtained on one thread and then used from another. var profile = await Task.Run(() =>
{
return NetworkInformation.GetInternetConnectionProfile();
});
myButton.Content = profile.GetNetworkConnectivityLevel(); |
@nickrandolph can you still reproduce this in WinUI? I tried your code that was failing and it now works. I am reverting our fix as it broke other areas, but it all seems to work now... I have my version numbers of things in the PR: dotnet/maui#10697 If this is still an issue, might be worth attaching your |
FYI @codendone this snippet is the one that gives me an exception |
Update: I've observed that this issue only happens on Win10. It works fine on Win11. I've got a VM setup with the behavior if the folks working on this need it. |
Describe the bug
Previously in UWP, calling the
NetworkInformation.GetInternetConnectionProfile()
method would return the network information - regardless of the thread used.In WinUI, this gives a COMException if it is not on a "main" thread.
Original issue: dotnet/maui#10062
Workaround: dotnet/maui#9972
Steps to reproduce the bug
NetworkInformation.GetInternetConnectionProfile();
on a background thread/task.Expected behavior
No exception
Screenshots
No response
NuGet package version
1.1.4
Packaging type
Packaged (MSIX)
Windows version
Windows 10 version 21H2 (19044, November 2021 Update)
IDE
Visual Studio 2022
Additional context
No response
The text was updated successfully, but these errors were encountered: