Immediate Termination of a BackgroundWorker Thread

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

    #1

    Immediate Termination of a BackgroundWorker Thread

    Is there a way to immediately terminate a BackgroundWorke r thread? My
    problem is that the BackgroundWorke r starts with a call to
    My.Computer.Fil eSystem.GetFile s that can run for a very long time if
    it is pointing to a directory tree with many files. If the user
    requests cancellation during this time, the main thread can call
    CancelAsync and can post a "Cancellati on Pending" message to the user,
    but the BackgroundWorke r cannot respond to the CancellationPen ding
    property until the call to My.Computer.Fil eSystem.GetFile s returns. It
    seems inelegant to rewrite the call to My.Computer.Fil eSystem.GetFile s
    to use calls to Dir or something that would allow me to poll the
    CancellationPen ding property while I'm building the collection of file
    names. And I have not wanted to rewrite the BackgroundWorke r as a
    standard thread (read that "go through the learning curve"!). I would
    rather abort the BackgroundWorke r thread but do not see how to do it.

    Thanks.

    redear
  • rowe_newsgroups

    #2
    Re: Immediate Termination of a BackgroundWorke r Thread

    On Feb 27, 7:37 pm, redear <red...@nowhere .com.invalidwro te:
    Is there a way to immediately terminate a BackgroundWorke r thread? My
    problem is that the BackgroundWorke r starts with a call to
    My.Computer.Fil eSystem.GetFile s that can run for a very long time if
    it is pointing to a directory tree with many files. If the user
    requests cancellation during this time, the main thread can call
    CancelAsync and can post a "Cancellati on Pending" message to the user,
    but the BackgroundWorke r cannot respond to the CancellationPen ding
    property until the call to My.Computer.Fil eSystem.GetFile s returns. It
    seems inelegant to rewrite the call to My.Computer.Fil eSystem.GetFile s
    to use calls to Dir or something that would allow me to poll the
    CancellationPen ding property while I'm building the collection of file
    names. And I have not wanted to rewrite the BackgroundWorke r as a
    standard thread (read that "go through the learning curve"!). I would
    rather abort the BackgroundWorke r thread but do not see how to do it.
    >
    Thanks.
    >
    redear

    If it's running in a seperate background thread, why do you need it to
    terminate immediately? After all what the user doesn't see can't hurt
    them (well, sorta :-) )

    Thanks,

    Seth Rowe

    Comment

    • Branco Medeiros

      #3
      Re: Immediate Termination of a BackgroundWorke r Thread

      redear wrote:
      Is there a way to immediately terminate a BackgroundWorke r thread? My
      problem is that the BackgroundWorke r starts with a call to
      My.Computer.Fil eSystem.GetFile s that can run for a very long time if
      it is pointing to a directory tree with many files. If the user
      requests cancellation during this time, the main thread can call
      CancelAsync and can post a "Cancellati on Pending" message to the user,
      but the BackgroundWorke r cannot respond to the CancellationPen ding
      property until the call to My.Computer.Fil eSystem.GetFile s returns.
      <snip>

      You may try using Thread.Abort, which will cause an exception to be
      thrown in the thread that you want to finish. You must call
      Thread.ResetAbo rt in you Catch or Finally block, or else the exception
      will bubble up...

      A few words of caution, though: thread synchronization is almost an
      art, and maybe you start getting more troubles than the one you want
      to solve, such as bad breath, too much sugar in your coffee or strange
      calls to your cell phone at 3 a.m. ...

      <code>
      'At file level:
      Imports SysThread = System.Threadin g
      Imports SysComps = System.Componen tModel

      '...
      'At form level:
      Private mThread As SysThread.Threa d

      Private Sub BGWorker_DoWork ( _
      ByVal Sender As Object, _
      ByVal E As SysComps.DoWork EventArgs _
      ) Handles BGWorker.DoWork

      mThread = SysThread.Threa d.CurrentThread
      Try
      For X As Integer = 0 To 100
      SysThread.Threa d.Sleep(1000)
      BGWorker.Report Progress(X)
      Next
      Catch Ex As SysThread.Threa dAbortException
      SysThread.Threa d.ResetAbort()
      End Try
      End Sub

      Private Sub BTNCancel_Click ( _
      ByVal Sender As Object, _
      ByVal E As System.EventArg s _
      ) Handles BTNCancel.Click

      If BGWorker.IsBusy AndAlso _
      mThread IsNot Nothing Then
      mThread.Abort()
      mThread = Nothing
      End If
      End Sub
      </code>


      In the code above, BGWorker is the background worker component, and
      BTNCancel is, as you may have guessed, a "cancel" button.

      HTH.

      Regards,

      Branco.

      Comment

      • redear

        #4
        Re: Immediate Termination of a BackgroundWorke r Thread

        On 28 Feb 2007 05:23:47 -0800, "Branco Medeiros"
        <branco.medeiro s@gmail.comwrot e:
        >redear wrote:
        >Is there a way to immediately terminate a BackgroundWorke r thread? My
        >problem is that the BackgroundWorke r starts with a call to
        >My.Computer.Fi leSystem.GetFil es that can run for a very long time if
        >it is pointing to a directory tree with many files. If the user
        >requests cancellation during this time, the main thread can call
        >CancelAsync and can post a "Cancellati on Pending" message to the user,
        >but the BackgroundWorke r cannot respond to the CancellationPen ding
        >property until the call to My.Computer.Fil eSystem.GetFile s returns.
        ><snip>
        >
        >You may try using Thread.Abort, which will cause an exception to be
        >thrown in the thread that you want to finish. You must call
        >Thread.ResetAb ort in you Catch or Finally block, or else the exception
        >will bubble up...
        >
        >A few words of caution, though: thread synchronization is almost an
        >art, and maybe you start getting more troubles than the one you want
        >to solve, such as bad breath, too much sugar in your coffee or strange
        >calls to your cell phone at 3 a.m. ...
        >
        ><code>
        'At file level:
        Imports SysThread = System.Threadin g
        Imports SysComps = System.Componen tModel
        >
        '...
        'At form level:
        Private mThread As SysThread.Threa d
        >
        Private Sub BGWorker_DoWork ( _
        ByVal Sender As Object, _
        ByVal E As SysComps.DoWork EventArgs _
        ) Handles BGWorker.DoWork
        >
        mThread = SysThread.Threa d.CurrentThread
        Try
        For X As Integer = 0 To 100
        SysThread.Threa d.Sleep(1000)
        BGWorker.Report Progress(X)
        Next
        Catch Ex As SysThread.Threa dAbortException
        SysThread.Threa d.ResetAbort()
        End Try
        End Sub
        >
        Private Sub BTNCancel_Click ( _
        ByVal Sender As Object, _
        ByVal E As System.EventArg s _
        ) Handles BTNCancel.Click
        >
        If BGWorker.IsBusy AndAlso _
        mThread IsNot Nothing Then
        mThread.Abort()
        mThread = Nothing
        End If
        End Sub
        ></code>
        >
        >
        >In the code above, BGWorker is the background worker component, and
        >BTNCancel is, as you may have guessed, a "cancel" button.
        >
        >HTH.
        >
        >Regards,
        >
        >Branco.
        Branco,

        Thanks for the clear example as well as the words of warning. Very
        helpful!

        redear

        Comment

        • TP

          #5
          Re: Immediate Termination of a BackgroundWorke r Thread

          I recommend you use FindFirstFile/FindNextFile/FindClose via
          pinvoke. That way you could periodically check for a pending
          cancel and then exit.

          For example, execute FindNextFile in a loop for 100 iterations,
          and then check for a pending cancel. If there is a pending cancel
          do a FindClose and exit the loop.

          -TP

          redear wrote:
          Is there a way to immediately terminate a BackgroundWorke r thread? My
          problem is that the BackgroundWorke r starts with a call to
          My.Computer.Fil eSystem.GetFile s that can run for a very long time if
          it is pointing to a directory tree with many files. If the user
          requests cancellation during this time, the main thread can call
          CancelAsync and can post a "Cancellati on Pending" message to the user,
          but the BackgroundWorke r cannot respond to the CancellationPen ding
          property until the call to My.Computer.Fil eSystem.GetFile s returns. It
          seems inelegant to rewrite the call to My.Computer.Fil eSystem.GetFile s
          to use calls to Dir or something that would allow me to poll the
          CancellationPen ding property while I'm building the collection of file
          names. And I have not wanted to rewrite the BackgroundWorke r as a
          standard thread (read that "go through the learning curve"!). I would
          rather abort the BackgroundWorke r thread but do not see how to do it.
          >
          Thanks.
          >
          redear

          Comment

          • redear

            #6
            Re: Immediate Termination of a BackgroundWorke r Thread

            On Wed, 28 Feb 2007 13:08:38 -0500, "TP"
            <tperson.knowsp amn@mailandnews .comwrote:
            >I recommend you use FindFirstFile/FindNextFile/FindClose via
            >pinvoke. That way you could periodically check for a pending
            >cancel and then exit.
            >
            >For example, execute FindNextFile in a loop for 100 iterations,
            >and then check for a pending cancel. If there is a pending cancel
            >do a FindClose and exit the loop.
            >
            >-TP
            TP,

            Thanks. That's what I am going to do. Looks like it is better practice
            than aborting a thread from the thread pool, even if it is a few more
            statements.

            Thanks.

            redear

            Comment

            Working...