This article is a mirror article of machine translation, please click here to jump to the original article.

View: 29278|Reply: 2

[Source] .net/c# Thread thread Suspend and Resume obsolete solutions

[Copy link]
Posted on 5/17/2018 9:36:10 AM | | | |
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:








Previous:C# sends text messages to mobile phones
Next:SQL removes spaces from the data
 Landlord| Posted on 5/17/2018 9:14:51 PM |
_event. Set(); is to continue the thread
_event. Reset(); is to suspend the thread
 Landlord| Posted on 5/29/2018 2:58:49 PM |
If you get the ID of the current thread, use :

Thread.CurrentThread.ManagedThreadId.ToString() (recommended)
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com