Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2005

Tip: Looking for answers? Try searching our database.

thread.abort and thread.suspend

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
am - 02 Sep 2005 10:06 GMT
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();
    }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.