thread.abort and thread.suspend

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • am

    thread.abort and thread.suspend

    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!

  • Lasse Vågsæther Karlsen

    #2
    Re: thread.abort and thread.suspend

    am wrote:[color=blue]
    > 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!
    >[/color]

    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

    mailto:lasse@vk arlsen.no
    PGP KeyID: 0x2A42A1C2

    Comment

    • SharpCoderMP

      #3
      Re: thread.abort and thread.suspend

      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:[color=blue]
      > 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!
      >[/color]

      Comment

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: thread.abort and thread.suspend

        Hi,

        Take a look at Skeet's article :


        cheers,

        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation


        "am" <am_public@emai l.it> wrote in message
        news:1125652034 .158916.170530@ g43g2000cwa.goo glegroups.com.. .[color=blue]
        > 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!
        >[/color]


        Comment

        • am

          #5
          Re: thread.abort and thread.suspend

          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 ManualResetEven t mResetEvent = new ManualResetEven t( 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.Res et();
          }

          /// <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 VerifySuspendSt op()
          {
          bool continueExecuti on = true;

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

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

          return continueExecuti on;
          }

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

          if ( !VerifySuspendS top )
          {
          return;
          }

          // time consuming code

          if ( !VerifySuspendS top )
          {
          return;
          }

          // time consuming code
          }
          }

          }

          class ClassThatUsesEn gine
          {
          public ClassThatUsesEn gine()
          {
          Engine engine = new Engine();

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

          thread.Start();

          thread.Join();
          }
          }

          Comment

          Working...