Terminating a thread from the main thread

Discussion in 'ASP .Net' started by Charles A. Lackman, Dec 9, 2004.

  1. Hello,

    I have created a thread that writes data from a memorystream to the
    harddrive. If the user, during the write decides to cancel the operation, I
    call Abort on the thread.

    i.e.

    Private Sub WriteToDiskButton_Click
    MyThread= New Threading.Thread(AddressOf WriteToDisk)
    MyThread.Start()
    End Sub

    Private Sub WriteToDisk
    Try
    'do stuff
    Catch Ex as System.Threading.ThreadAbortException
    End Try
    End Sub

    Private Sub AbortButton_Click
    MyThread.Abort
    End Sub

    When the Abort takes place I get two Abort Exceptions, I am able to catch
    the fist one but how do I catch both exceptions so the user is not aware of
    them.

    Chuck
    Charles A. Lackman, Dec 9, 2004
    #1
    1. Advertising

  2. I'd suggest not using Thread.Abort to terminate the thread. Ian Griffiths
    writes about why Thread.Abort is "evil" and how to stop a thread properly.

    http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation

    "Charles A. Lackman" wrote:

    > Hello,
    >
    > I have created a thread that writes data from a memorystream to the
    > harddrive. If the user, during the write decides to cancel the operation, I
    > call Abort on the thread.
    >
    > i.e.
    >
    > Private Sub WriteToDiskButton_Click
    > MyThread= New Threading.Thread(AddressOf WriteToDisk)
    > MyThread.Start()
    > End Sub
    >
    > Private Sub WriteToDisk
    > Try
    > 'do stuff
    > Catch Ex as System.Threading.ThreadAbortException
    > End Try
    > End Sub
    >
    > Private Sub AbortButton_Click
    > MyThread.Abort
    > End Sub
    >
    > When the Abort takes place I get two Abort Exceptions, I am able to catch
    > the fist one but how do I catch both exceptions so the user is not aware of
    > them.
    >
    > Chuck
    >
    >
    >
    =?Utf-8?B?SGFhY2tlZA==?=, Dec 9, 2004
    #2
    1. Advertising

  3. Charles A. Lackman

    Cor Ligthert Guest

    Charles,

    I think it is better to fine out why the exception is throwed, that should
    not happen with just an abortion of a thread.

    A try and catch is for situations when you cannot find that.

    By instance when you know that a file exist on a shared disk, however don't
    know if it is in use already.

    Just my thought,

    Cor

    "Charles A. Lackman" <>
    ....
    > Hello,
    >
    > I have created a thread that writes data from a memorystream to the
    > harddrive. If the user, during the write decides to cancel the operation,
    > I call Abort on the thread.
    >
    > i.e.
    >
    > Private Sub WriteToDiskButton_Click
    > MyThread= New Threading.Thread(AddressOf WriteToDisk)
    > MyThread.Start()
    > End Sub
    >
    > Private Sub WriteToDisk
    > Try
    > 'do stuff
    > Catch Ex as System.Threading.ThreadAbortException
    > End Try
    > End Sub
    >
    > Private Sub AbortButton_Click
    > MyThread.Abort
    > End Sub
    >
    > When the Abort takes place I get two Abort Exceptions, I am able to catch
    > the fist one but how do I catch both exceptions so the user is not aware
    > of them.
    >
    > Chuck
    >
    Cor Ligthert, Dec 9, 2004
    #3
  4. Charles A. Lackman schrieb:
    > Private Sub AbortButton_Click
    > MyThread.Abort
    > End Sub
    >
    > When the Abort takes place I get two Abort Exceptions, I am able to catch
    > the fist one but how do I catch both exceptions so the user is not aware of
    > them.


    Add a handler to 'System.Windows.Forms.Application.ThreadException' in
    order to catch the exceptions.

    --
    Herfried K. Wagner [MVP]
    <URL:http://dotnet.mvps.org/>
    Herfried K. Wagner [MVP], Dec 9, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Uppu
    Replies:
    0
    Views:
    344
  2. Jeffrey Barish
    Replies:
    0
    Views:
    293
    Jeffrey Barish
    May 28, 2004
  3. DE
    Replies:
    3
    Views:
    379
  4. Ian Wilson
    Replies:
    4
    Views:
    626
    Ian Wilson
    Jan 24, 2007
  5. Hugo
    Replies:
    4
    Views:
    1,710
    Logan Shaw
    Mar 27, 2008
Loading...

Share This Page