How long will it wait? [Fernando Vicaria]

This posting will touch once again the Process class and the WaitForInputIdle method.

This method contains 2 overloads, one that takes no parameters and another one that will take an int as its single parameter. The confusion will start exactly here with what these two overloads do differently. The first one will wait indefinitely for the process to enter an idle state locking your app until it returns when the process in question has become idle.

Inside the process class these two overloads map to only one call to the Windows API WaitForInputIdle located in User32.lib. The signature of this API is as follows:

DWORD WaitForInputIdle(
  HANDLE hProcess,
  DWORD dwMilliseconds

where hProcess is the handle to the process main window and dwMilliseconds the time-out interval, in milliseconds, we want to wait for.

When calling the parameterless version of the managed method what we are really doing is passing Int32.MaxValue as the parameter for the other one. In this case the value of this constant is 2,147,483,647; that is, hexadecimal 0x7FFFFFFF, which for all effects mean wait indefinitely. Now back to the unmanaged world there was a more explicit way to tell the API to wait forever; We would simply use INFINITE, which in windows.h maps to 0xfffffffh (or -1 if you prefer).

Another special value that we can use when calling the WaitForInputIdle(int) method in the process class is 0 (ZERO). This will cause the method to return true or false immediately.

So as a summary when calling Process.WaitForInputIdle(int) we have:

  • -1 = wait indefinitely (any other negative number will be read as a DWORD)
  •  0 = return immediately
  • 1 to Int32.MaxValue = wait for the specified amount of time (in milliseconds)

Comments (2)

  1. Monroe Thomas says:

    Your decision to pass int.MaxValue to unmanaged WaitForInputIdle from the parameterless version of the API seems incorrect, since it will return after 77 days of inactivity. This is NOT THE SAME as waiting indefinitely. Many embedded and remote systems that we build can go months without any user interaction. If the documentation for the parameterless API says that it will wait indefinitely, then you should pass -1 to the unmanaged API, not int.MaxValue.

Skip to main content