title: UWP AppDiagnostic: System.AccessViolationException
body: <p>I am running a windows service (C#, .NET Framework 4.7.2) which manages a UWP app.</p>
  <p>Among required features, I need to know if the UWP app is running.</p>
  <p>To know the list of running Windows Store Apps, I use the following code:
  (based on <a href="https://blogs.windows.com/windowsdeveloper/2017/06/28/uwp-app-diagnostics/" rel="nofollow noreferrer">https://blogs.windows.com/windowsdeveloper/2017/06/28/uwp-app-diagnostics/</a>)</p>
  <pre><code>private static async Task&lt;List&lt;AppDiagnosticInfo&gt;&gt; GetWindowsStoreApps()
  {
      var apps = new List&lt;AppDiagnosticInfo&gt;();
  
      var diagnosticAccessStatus = await AppDiagnosticInfo.RequestAccessAsync();
      if (diagnosticAccessStatus != DiagnosticAccessStatus.Allowed)
          return apps;
  
      var processes = ProcessDiagnosticInfo.GetForProcesses();
  
      foreach (var process in processes)
      {
          try
          {
              if (!process.IsPackaged)
                  continue;
  
              IList&lt;AppDiagnosticInfo&gt; diagnosticInfos = process.GetAppDiagnosticInfos();
              if (diagnosticInfos == null || diagnosticInfos.Count &lt; 1)
                  continue;
  
              apps.Add(diagnosticInfos[0]);
          }
          catch
          {
              continue;
          }
      }
  
      return apps;
  }
  </code></pre>
  <p>I call this method on a regular basis with a Timer. Eventually it will crash the service with the following exception (the try{} catch{} doesn't help).</p>
  <p><strong>Exception thrown: 'System.AccessViolationException' in ...
  An unhandled exception of type 'System.AccessViolationException' occurred in ...</strong></p>
  <p>This occurs when trying to access IsPackaged property.</p>
  <p>Why is it crashing? Is it because it's not run from within a UWP app?
  Does someone know any alternative to retrieve the list of UWP apps (names and process ids)?</p>
  <p>Thanks for your help!</p>