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

NetworkInformation.GetInternetConnectionProfile() throws on background threads #2965

Open
mattleibow opened this issue Sep 13, 2022 · 8 comments
Assignees

Comments

@mattleibow
Copy link

mattleibow commented Sep 13, 2022

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

  1. Call 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

@ghost ghost added the needs-triage label Sep 13, 2022
@nickrandolph
Copy link

nickrandolph commented Sep 22, 2022

@mattleibow this doesn't repro with the minimal steps provided. For example, this works:

  • Create new WinAppSdk/WinUI app
  • add following code to button click handler, which runs without exception:
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();
}

@codendone
Copy link
Contributor

I don't repro this issue. Is there anything specific about how you've created the thread? I tried this:

        private void myButton_Click(object sender, RoutedEventArgs e)
        {
            myButton.Content = "Clicked";
            var thread = new Thread(new ThreadStart(WorkThreadFunction));
            thread.Start();
        }

        public void WorkThreadFunction()
        {
            var profile = NetworkInformation.GetInternetConnectionProfile();
            System.Diagnostics.Debug.WriteLine("Network: profile name: " + profile.ProfileName + " adapter: " + profile.NetworkAdapter);
        }

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.

@mattleibow
Copy link
Author

I believe the thread was created using the Task.Run() code.

@mattleibow
Copy link
Author

mattleibow commented Sep 26, 2022

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();

@mattleibow
Copy link
Author

mattleibow commented Oct 14, 2022

@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 obj\project.assets.json and OS version so we can see exactly what system you have.

@HelenMamalaki
Copy link

HelenMamalaki commented Oct 25, 2023

I am having the same issue:
Target framework: net7.0-windows10.0.19041.0

  "Microsoft.Maui.Dependencies/6.0.312": {
    "type": "package",
    "dependencies": {
      "Microsoft.Graphics.Win2D": "1.0.3.1",
      "Microsoft.Maui.Graphics": "6.0.300",
      "Microsoft.Maui.Graphics.Win2D.WinUI.Desktop": "6.0.300",
      "Microsoft.WindowsAppSDK": "1.0.3"
    }

project.assets.json

Laptop running: Windows 10 Enterprise 19045.3570

I am not using Task.Run or anything, I am using an await on an async service class which has the internet check inside.

I see the MAUI work around issue is "Closed" however the merged fix was reverted.

EDIT
I can't keep calling my fetch data API classes on the Main thread as it blocks the UI thread - this seems like a major issue.
image

@Foda
Copy link
Member

Foda commented Nov 29, 2023

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();

FYI @codendone this snippet is the one that gives me an exception

@Foda
Copy link
Member

Foda commented Dec 4, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants