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.
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
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:
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
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.