ManagementObjectSearcher does not work within global hook


When I try to get all running processes inside mouse event handler it throws an exception. First I thought that the problem persists because I put async keyword before mouse event handler, but it was not the case, as the exception is thrown also for non-asynchronous methods.

I'm using MouseKeyHook library.

Exception message:

Additional information: Transition into COM context 0x1ac936a0 for this RuntimeCallableWrapper failed with the following error: An outgoing call cannot be made since the application is dispatching an input-synchronous call. (Exception from HRESULT: 0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL)).

Event handler from which I'm getting all processes:

private async void MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    List<ProcessInfo> allRunningProcesses = Logic.GetAllProcesses();
    // ...

Get all processes by using ManagementObjectSearcher:

public static List<ProcessInfo> GetAllProcesses()
     using (var searcher = new ManagementObjectSearcher(wmiQueryString))
            using (var results = searcher.Get()) // EXCEPTION THROWN!
                // ...

As you can see the exception is thrown when calling searcher.Get(). Note: This method works without any issues if used outside the mouse event handler (MouseUp).

asked on Stack Overflow Aug 3, 2016 by Mr. Blond • edited Jun 28, 2019 by halfer

1 Answer


As it turns out, COM requires you to run your code on STA if there is MTA involved and you are using the ManagementObjectSearcher methods within SendMessage().
So, what I needed to do is to run my code on differet thread and set SetApartmentState to ApartmentState.STA.

List<ProcessInfo> allRunningProcesses = null;

Thread threadProc = new Thread(() =>
    allRunningProcesses = Logic.GetAllProcesses();


Useful links:

msdn- Understanding and Using COM Threading Models
stackoverflow- How to run something in the STA thread

answered on Stack Overflow Aug 6, 2016 by Mr. Blond • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0