Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello,
In my work I use a thread to read from a serial port and then insert into database. Sometimes i need to pause this operation and after that continue it... I previously used Thread.Suspend(); And Thread.Resume(); to suspend and resume the work done by the thread.

But Now while compiling it says Thread.Suspend(); And Thread.Resume(); are obsolete and deprecated.

How can i do my work without using Thread.Suspend(); Or Thread.Resume();

Is there any way to suspend and resume work..??
Posted
Updated 22-Mar-12 1:12am
v2

 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 22-Mar-12 13:28pm    
This is exactly what should be used. My 5.
--SA
Shahin Khorshidnia 22-Mar-12 13:39pm    
Thank you SA :)
Sergey Alexandrovich Kryukov 22-Mar-12 13:38pm    
However, I decided to add my answer to explain why the way you mention is good and safe, unlike deprecated thread suspension.

Please see.
--SA
I would like to add to the correct answer by Shahin. Using WaitOne if free from the problems of thread suspensions. If an instance of an event wait handle is not set, the calling thread waits in a special wait state. OS switches the calling thread off and do not schedule it back to execution until it is "awaken" by some reason. In this state, the waiting thread wastes zero CPU time.

The waiting thread can be awaken on different conditions. The primary condition is setting the instance of the same even handle in some other thread; which is the main purpose of this thread synchronization API. But there are other conditions: expired timeout (if applied) or calling Thread.Abort for the waiting thread, from some other thread. This way, there is no danger that the waiting thread blocks the whole process from termination and any other bad thing.

—SA
 
Share this answer
 
v4
Comments
Shahin Khorshidnia 22-Mar-12 13:41pm    
It's a perfect explanation (My +5)
Sergey Alexandrovich Kryukov 22-Mar-12 13:58pm    
Thank you, Shahin.
Teamwork! :-)
--SA
Shahin Khorshidnia 22-Mar-12 14:31pm    
Yes, It makes me proud ;)
Sergey Alexandrovich Kryukov 22-Mar-12 15:24pm    
:-)
These methods are deprecated for a reason. It can wreak havoc on your AppDomain using Thread.Suspend. Please read about it here: http://msdn.microsoft.com/en-us/library/tttdef8x.aspx[^]. I don't know what exactly you are trying to achieve, so maybe you'd better start by explaining what you are driving at. Use the link "Improve question" beneath your question to edit it and add the nescessary details.

Regards,

Manfred
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Mar-12 13:39pm    
Good point, but apparently OP already knew that. So, what's the alternative?

There is a right alternative, and Shahin mentioned it in his answer, and I explained why this is safe and good (unlike deprecated API) in my answer.

Please see them.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900