Suspend all threads in the current process at runtime

I have a bug in my application that seems to show its face only when I pause the application in the debugger for several minutes. I suspect this is due to a third-party network library that I'm using, with a heartbeat thread that disconnects when it cannot ping the server while the interrupt thread is interrupted.

I am trying to write a testing application to make sure that this is the cause of the error. To do this, I need a way to pause all threads in the application (which I judge later, to pause only the thread, which I suspect might be a heartbeat thread) to simulate pausing the application in the debugger.

Does anyone know how to do this? Is it possible for one thread to make another sleep?

Thanks Alex

UPDATE:

In the end, I decided that I really did not need the application to do this for me, because I just had to check that the suspension in the debugger caused a break. So, here's what I did ... (The simplest ways are often the best ... or at least the simplest ...)

private static void Main(string[] args) { IPubSubAdapter adapter = BuildAdapter(); bool waitingForMessage; adapter.Subscribe(_topic, message => waitingForMessage = false, DestinationType.Topic); Stopwatch timePaused = new Stopwatch(); while (adapter.IsConnected) { Console.WriteLine("Adapter is still connected"); waitingForMessage = true; adapter.Publish(_topic, "testmessage", DestinationType.Topic); while (waitingForMessage) { Thread.Sleep(100); } timePaused.Reset(); timePaused.Start(); Debugger.Break(); timePaused.Stop(); Console.WriteLine("Paused for " + timePaused.ElapsedMilliseconds + "ms."); Thread.Sleep(5000); // Give it a chance to realise it disconnected. } Console.WriteLine("Adapter is disconnected!"); Console.ReadLine(); } 

And the conclusion:

 Adapter is still connected Paused for 10725ms. Adapter is still connected Paused for 13298ms. Adapter is still connected Paused for 32005ms. Adapter is still connected Paused for 59268ms. Adapter is disconnected! 
+7
multithreading c # heartbeat tibco-ems
source share
5 answers

You can use this to quickly identify your process threads:

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

Then you can use kernel32.dll with P / Invoke to do everything you need with these threads. Use OpenThread to get the descriptor of the desired thread, and then pause it with SuspendThread using this descriptor.

Here is the P / Invoke declaration for two methods:

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

You can pause threads by calling Thread.Suspend . The documentation gives a big "DON'T DO IT!". warnings, but I believe that your valid use case.

Jon Skeet believes that you cannot list managed threads in normal C # code, although it alludes to a possible solution.

+1
source share

I assume that you will not stop all threads in the application, otherwise nothing will be done to cancel them. Or am I missing something?

Suggestion: try naming all the ones you create. Any streams without a name, or that do not comply with your naming convention, must be created by a third-party component. This can lead to the first reason faster without having to suspend many threads.

+1
source share

From my pov it doesn’t sound like that.

  • What tests are you writing? If you are talking about unit tests, then this is not the case of Unit test - more like an integration test
  • Think about isolating API calls in a class and then using dependency injection so that you can test without a third-party library using mocks / stubs, and also trigger / test an exception raised from a third-party library
+1
source share

You can call the Thread.Resume then Thread.Resume , but they are deprecated and it is not recommended to use them.

But you can do the following: Have a boolean flag that, when set, puts your thread in big sleep. OR It is better to use ManualResetEvent .

0
source share

All Articles