Thread.Suspend has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources. The hyperlink login is visible. Thread.Resume has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources. The hyperlink login is visible. If the thread wants to pause or continue, I have used the two methods of Suspend and Resume before, and use these two methods to operate the thread, which is actually problematic, and I didn't care about it at the time, so let's take it out and study it today!
What is the problem with using the above two methods to suspend and continue threads?
For example, if we call the Suspend method to suspend the thread, and then immediately call the Resume method to continue the suspended thread, then there may be a problem.
Because, after you call the Suspend method to suspend the thread, the thread may not be suspended immediately, here it takes processing time, at this time, you immediately call the Resume method to continue the suspended thread, and the program will hang immediately!
Microsoft's explanation is:
Do not use the Suspend and Resume methods to synchronize thread activity. Is there a way to know what code is when you pause the execution of the thread. If you hold a lock during security privilege evaluation, other threads in your suspended thread in AppDomain may be blocked. If you suspend other threads in the AppDomain when executing the class constructor, attempts to use the class are blocked. Deadlocks can easily occur.
// Summary: Continue the suspended thread. // Anomaly: // T:System.Threading.ThreadStateException: The thread is not started, dead, or is not in a pending state. // // T:System.Security.SecurityException: The caller does not have the proper System.Security.Permissions.SecurityPermission. [Obsolete("Thread.Resume has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources. The hyperlink login is visible.", false)] [SecuritySafeCritical] public void Resume(); How to solve this problem?
ManualResetEvent class
Notify one or more waiting threads that an event has occurred. Such types cannot be inherited.
The test code is as follows:
|