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!
Jan - 02 Sep 2005 10:42 GMT
Hey am
You could try communicating with the threads using a ManualRestEvent.
That way the threads could listen for your signal and then terminate
themselves.
Jan
am - 02 Sep 2005 10:58 GMT
Sorry, I can't understand how I should do. The thread should work until
I need to suspend or abort it (only if I need, otherwise it should
continue until it ends). With ManualResetEvent the thread would wait
the signal, doing nothing. Does it?
Damien - 02 Sep 2005 13:37 GMT
> Sorry, I can't understand how I should do. The thread should work until
> I need to suspend or abort it (only if I need, otherwise it should
> continue until it ends). With ManualResetEvent the thread would wait
> the signal, doing nothing. Does it?
e.g. (VB Code)
Private Sub ThreadCode()
While not abortEvent.WaitOne(0,False) andalso 'Other conditions for
continuing to work
'Do all kinds of things
End While
'If necessary, do different cleanup here depending on abort/run out
of things to do. Better done by checking that work has finished rather
than by Waiting again, since abort could be signalled after you're run
out of work, but before reaching here
'Better still, arrange your code such that it doesn't matter which
caused the thread to finish.
End Sub
Under some circumstances, it may be appropriate for some inner loops to
also check the abort event whilst they're running - if it's safe for
them to finish with not all of their work complete.
Damien
Cowboy (Gregory A. Beamer) - MVP - 02 Sep 2005 14:51 GMT
The push away from Suspend and Abort are natural, as a thread suspending or
aborting can end creating a deadlock situation. My advice would be to learn
about Mutex and Monitor. When you use these classes, you can do what you
desire safely.

Signature
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
***************************
Think Outside the Box!
***************************
> 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!
am - 02 Sep 2005 15:12 GMT
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();
}
}