.comment-link {margin-left:.6em;}

.code

.. Hello Earthling .. \o/ :)

Dont Use Thread.Suspend() Method
Thursday, November 17, 2005

The Thread.Suspend documentation puts a nice easy to understand note:

"Do not use the Suspend and Resume methods to synchronize the activities of threads. You have no way of knowing what code a thread is executing when you suspend it. If you suspend a thread while it holds locks during a security permission evaluation, other threads in the AppDomain might be blocked. If you suspend a thread while it is executing a class constructor, other threads in the AppDomain that attempt to use that class are blocked. Deadlocks can occur very easily."

When you write "Thread.CurrentThread.Suspend( );" in the editor & build, you'll get the following warning:

'System.Threading.Thread.Suspend()' is obsolete: '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. http://go.microsoft.com/fwlink/?linkid=14202'


Comments:
do you know a way around this, or how to resolve this issue with the suspend and resume methods?
could you post some sample code or give me a site that will fix this error for me?
much appreciated
new guy
 
Hi, this *issue* cannot be fixed by just any of the available methods. Its a problem of thread synchronisation. The document, as I wrote, points out clearly that you should use the classes available in System.Threading such as Event etc. Now it can be confusing to understand incase you are new to thread synchronization. In that case you should look for articles on internet related to thread synchronisation in C#/vb.net. Regarding thread.suspend: its wrong, in the first place, to suspend your thread through a thread.suspend or a sleep statement just because you *expect* something to have happened by the time your wait time expires. For example:
while (true)
{
Sleep (1000); // or a suspend statement here
if (something_done)
break;

}
I would say its a wrong design. Instead you should have some thread synchronisation object on which you could wait, and that wait should be released by the task on which you are waiting. I know how this kind of code is written in c++ but I dont clearly remember what classes to use in C# since i havnt worked on it for quite some time (but Event class is one). I dont promise to write a blog post on this but I can say I'll try. But dont count on it :)
 
In principle, a good happen, support the views of the author
 
Post a Comment

Links to this post:

Create a Link



<< Home



Blogs:

MSDN Blogs
Joel Pobar
Don Syme

Friends:
Adeel
Aqeel
#Fahad
Haroon
Omer
Muhammad Ali
Lahore Food Blog

Links:
Rotor
CodeGuru
Mozilla
OpenSourceNokia
Tech Blog

Languages:
IronPython
F#

Archives

June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   February 2005   April 2005   June 2005   July 2005   September 2005   October 2005   November 2005   June 2007   December 2007   January 2008   March 2008   April 2008   June 2008   October 2008   February 2009   May 2009   June 2009   July 2009   August 2009   March 2011   June 2011   July 2011   September 2011   October 2011   November 2011   December 2012   May 2013   October 2013   May 2014   March 2015   July 2015   August 2015   December 2015   March 2016   July 2016   October 2016   November 2016  

This page is powered by Blogger. Isn't yours?