Search code examples
c#windows-10win-universal-appwindows-10-universal

UWP_ Cannot click "yes" or "no" on "access diagnostics information" dialog to grant permission for app


My app need permission to get list of running processes. When I click a button to acquire list of processes for the first time, a dialog should be showed to ask for permission. But I cannot click yes or no button therefore cannot grant permission to my app. My app freezes.
enter image description here Below is my code:
appmanifest:

<Package>   xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities">
    <Capabilities>
        <Capability Name="internetClient" />
        <rescap:Capability Name="appDiagnostics"/>
      </Capabilities>
</Package>

Mainclass:

   private void LoadProcesses()
    {
                    List<ProcessDiagnosticInfo> processList = ProcessDiagnosticInfo.GetForProcesses().ToList();
                    processList.ForEach(o =>  Debug.WriteLine(o.ExecutableFileName));
    }

private  void StartButton_Click(object sender, RoutedEventArgs e)
{
   LoadProcesses();
}     

Do I miss something?

UPDATE
I've tried

   IList<AppDiagnosticInfo> listAppInfo = await AppDiagnosticInfo.RequestInfoAsync();

and it works fine, I can grant permission to my app. So get app info first then get processes info, the app can work. However it does not make sense when I only want to get process info. What do you think, leave me your comment. Thanks


Solution

  • The problem is that you're calling ProcessDiagnosticInfo.GetForProcesses() synchronously on the UI thread. This blocks waiting on the permissions dialog, which is blocked waiting on your GetForProcesses call: Deadlock.

    Two ways to fix this:

    1. Once permission is granted once that permission will persist until the user retracts it. You can request it asynchronously through AppDiagnosticInfo either indirectly as you do with your call to RequestInfoAsync or explicitly by calling AppDiagnosticInfo.RequestAccessAsync

    2. Call GetForProcesses off of the UI thread:

      await Task.Run(() =>
      {
          List<ProcessDiagnosticInfo> processList = ProcessDiagnosticInfo.GetForProcesses().ToList();
          processList.ForEach(o => Debug.WriteLine(o.ExecutableFileName));
      });
      

    The deadlock is logged as a bug to be addressed at the OS level in future versions of Windows 10. You can watch for it in Insider Previews.