I cannot run P / Invoke until SuspendThread, because there is no one-to-one mapping between managed threads and native threads
You also cannot list managed threads, only unmanaged threads. In fact, there is a one-to-one comparison between them, it is simply difficult for them to find it. The initial intention was to allow the creation of a custom CLR host that did not use operating system threads to implement Thread, querying the SQL Server group that wanted to use fibers instead. It never worked out, they couldn't get it reliable enough. There is no real CLR host that does not use real operating system threads.
So you can actually use Process.GetCurrentProcess (). Topics for listing all of your threads. And don't suspend your own by calling GetCurrentThreadId (), comparing it to ProcessThread.Id
How reliable this conjecture will be, do not try to do anything radical, like sending a warning, to remind you that the time has come to connect the debugger. You may well have paused a thread that was executing code inside Windows and acquired a global lock. Like a CLR worker thread, such as a finalizer thread or a background GC thread.
The best approach is to use a separate defense process that does all this, just like a debugger. Use the named EventWaitHandle that you create in the security program and OpenExisting () in your main program. The security program needs WaitAny () on this wait descriptor, as well as in the process. Now your main program can simply call Set () to wake up the security program. You can now safely pause all threads.
source share