How to prevent application from freezing while checking permissions using WMI

I use WMI, I need to get some information, but when the class is unavailable due to insufficient permissions, everything freezes for several (~ 5) seconds. Even setting a low timeout does not work (not to mention that it would be a stupid solution).

The problem is not in insufficient permissions, the problem is "freezing".

Is there a way to check if the current process has the right to read information from any class in order to prevent the exception “freezing” and “denied access”?

ConnectionOptions co = new ConnectionOptions();
co.Impersonation = ImpersonationLevel.Impersonate;
co.Authentication = AuthenticationLevel.Packet;
co.Timeout = new TimeSpan(0, 0, 1); // 1 second, but still hangs for ~5
co.EnablePrivileges = false; // false or true, doesn't matter
ManagementPath mp = new ManagementPath();
mp.NamespacePath = @"root\CIMV2\Security\MicrosoftTpm";
mp.Server = "";

ManagementScope ms = new ManagementScope(mp, co);
ms.Connect(); // Hangs for ~5 seconds and throws "access denied"
+3
source share
1 answer

, . - ( ), , . - :

  Task.Factory.StartNew(() =>
  {
    // Your permission checking code here.....
  }).ContinueWith((t) =>
  {
    // Inform user of permissions status.
  });

, "", BackgroundWorker. , .

+1

All Articles