469,622 Members | 1,445 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,622 developers. It's quick & easy.

thread.abort and thread.suspend

am
Hi to all.
I have a little problem. I'm working with threads, and I need to abort
or suspend them, but many experts dissuade from use Thread.Abort and
Thread.Suspend. As I didn't find other way, how can I do?
Thanks a lot!

Nov 17 '05 #1
4 10702
am wrote:
Hi to all.
I have a little problem. I'm working with threads, and I need to abort
or suspend them, but many experts dissuade from use Thread.Abort and
Thread.Suspend. As I didn't find other way, how can I do?
Thanks a lot!


Usually you're suspending the thread in order to wait for something to
do and to do that you're better off using one of the synchronizing
objects (like the event objects) and wait for the events to become
signalled.

As for aborting a thread, that has a whole host of problems associated
with it (with Thread.Abort that is) and a better way is to look at an
event object to become signalled and then simply return from the thread
method.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #2
hi,

i had this issue with aborting thread:
my separate thread was generating bitmap thumbnails from larger bimap
files. when i was aborting this thread it sometimes happened that bitmap
objects were not disposed immediately, so i ended up with 200-300 megs
of memory ussage.
someone suggested that i should create some sort of stop signal value
which is frequently checked by the thread. if the value is set to true
then thread skips all its job and exits.
this works well but has one problem - if operation performed by thread
is very time consuming, like reading or writing large files or
resampling bimaps *AND* this is done in one line of code, then you cant
cancel this operations with metioned stop signal. so you may end up
waiting for the operation to finish. offcourse with stop signal you end
up with clean exit (you can free all resources), but you cant cancel a
single operation this way.
and for that i didn't found a sollution :(

am wrote:
Hi to all.
I have a little problem. I'm working with threads, and I need to abort
or suspend them, but many experts dissuade from use Thread.Abort and
Thread.Suspend. As I didn't find other way, how can I do?
Thanks a lot!

Nov 17 '05 #3
Hi,

Take a look at Skeet's article :
http://www.yoda.arachsys.com/csharp/threads/abort.shtml

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"am" <am*******@email.it> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi to all.
I have a little problem. I'm working with threads, and I need to abort
or suspend them, but many experts dissuade from use Thread.Abort and
Thread.Suspend. As I didn't find other way, how can I do?
Thanks a lot!

Nov 17 '05 #4
am
Thanks a lot to all!
Now I will post my solution. If you find something wrong tell me
please. Thanks a lot again.

class Engine
{

private ManualResetEvent mResetEvent = new ManualResetEvent( true );
private volatile bool mStopExecution;

protected bool StopExecution
{
get { return mStopExecution; }
}

/// <summary>
/// Stops as soon as possible
/// </summary>
protected internal void Stop()
{
mStopExecution = true;

// restart thread in case it was suspended before stop request
Restart();
}

/// <summary>
/// Suspends thread
/// </summary>
protected internal void Suspend()
{
// set the monitor to not segnaled if thread is not requested to stop
if ( !StopExecution )
mResetEvent.Reset();
}

/// <summary>
/// Restart suspended thread
/// </summary>
protected internal void Restart()
{
// segnales monitor
mResetEvent.Set();
}

/// <summary>
/// This is the function used as argument of new ThreadStart() in the
constractor of
/// new Thread
/// </summary>
protected internal void Start( )
{
try
{
ExecControl();
}
catch ( Exception ecc )
{
mExitStatus = ExitStatusType.Errors;
mError = ecc;
}
finally
{
Done();
}
}

/// <summary>
/// Looks for suspend or stop requests.
/// </summary>
protected bool VerifySuspendStop()
{
bool continueExecution = true;

TimeSpan timeout = new TimeSpan( 3, 0, 0 );

if ( !mResetEvent.WaitOne( timeout, false ) )
{
mResults = "Timeout occured.";
continueExecution = false;
mExitStatus = ExitStatusType.TimeoutSuspend;
}
else if ( StopExecution )
{
mResults = "Thread stopped by user";
continueExecution = false;
mExitStatus = ExitStatusType.Stopped;
}

return continueExecution;
}

/// <summary>
/// Here I do what I want.
/// </summary>
protected void ExecControl()
{
while( VerifySuspendStop )
{
// quick code

if ( !VerifySuspendStop )
{
return;
}

// time consuming code

if ( !VerifySuspendStop )
{
return;
}

// time consuming code
}
}

}

class ClassThatUsesEngine
{
public ClassThatUsesEngine()
{
Engine engine = new Engine();

Thread thread = new Thread( new ThreadStart( engine.Start ) );

thread.Start();

thread.Join();
}
}

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Guido Kraus | last post: by
20 posts views Thread by Doug Thews | last post: by
18 posts views Thread by Urs Vogel | last post: by
22 posts views Thread by Brett | last post: by
6 posts views Thread by Robert Speck | last post: by
10 posts views Thread by Jon Slaughter | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.