Pausing all threads in current process at run time Pausing all threads in current process at run time multithreading multithreading

Pausing all threads in current process at run time


You can use this to quickly indentify the threads of your process:

using System.Diagnostics;ProcessThreadCollection threads = Process.GetCurrentProcess().Threads;

Then you can use kernel32.dll with P/Invoke to do whatever you need with those threads. Use OpenThread to get a handle to the desired thread and then suspend it with SuspendThread using that handle.

Here are the P/Invoke declaration for the two methods:

[DllImport("kernel32.dll")]static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);[DllImport("kernel32.dll")]static extern uint SuspendThread(IntPtr hThread);


I assume you're not going to pause all the threads in the application, otherwise there will be nothing running to un-suspend them. Or have I missed something?

A suggestion: Try giving names to all the threads you create. Any threads without a name, or that don't match your naming convention, must have been create by a third-party component. This might get you to the root cause quicker without having to pause lots of threads.


From my pov this sounds not right.

  • Which kind of tests are you writing? If you are speaking about unit tests, then this is not a Unit test case - sounds more like integration test
  • Consider to isolate the API calls in a class and then use dependendcy injection so that you can test without the 3rd party library with mocks/stubs and can also provoke/test an exception raised from the 3rd party library