Results 1 to 3 of 3

Thread: Why is Thread.Abort not doing anything?

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Why is Thread.Abort not doing anything?

    I expect MsgBox("Exception HIT!") to be reached ... but it is not ...

    Any ideas why this it isn't hit (and the thread is never aborted)?? ... The error doesn't even appear to have been raised (going to ExceptionSettings and ticking the ThreadAbortException).

    Thanks in advance,
    Kris
    VB.Net Code:
    1. Public Class Form1
    2.     Const ConString = "Data Source=dev-dbserver;Initial Catalog=kb_2430;Integrated Security=True"
    3.     Dim t As New System.Threading.Thread(Sub()
    4.                                              Try
    5.                                                  Using con As New System.Data.SqlClient.SqlConnection(ConString)
    6.                                                      con.Open()
    7.                                                      Using cmd = con.CreateCommand()
    8.                                                          cmd.CommandText = "WHILE 0 = 0 WAITFOR DELAY '01:00:00'"
    9.                                                          cmd.ExecuteNonQuery()
    10.                                                      End Using
    11.                                                  End Using
    12.  
    13.                                                  'Using the below instead works as expected:
    14.                                                  'System.Threading.Thread.Sleep(Integer.MaxValue)
    15.                                              Catch ex As Threading.ThreadAbortException
    16.                                                  MsgBox("Exception HIT!")
    17.                                              Catch ex As Exception
    18.                                                  MsgBox("Unhandled: " & ex.Message)
    19.                                              End Try
    20.  
    21.                                          End Sub)
    22.  
    23.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    24.         t.Start()
    25.  
    26.         System.Threading.Thread.Sleep(2500)
    27.  
    28.         t.Abort()
    29.     End Sub
    30. End Class

    EDIT:
    I suspect it may need to finish executing native code before it can terminate a thread??.. but just a guess

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Why is Thread.Abort not doing anything?

    Quote Originally Posted by i00 View Post
    I suspect it may need to finish executing native code before it can terminate a thread??.. but just a guess
    You don't need to guess, given that the documentation says this:

    If Abort is called on a managed thread while it is executing unmanaged code, a ThreadAbortException is not thrown until the thread returns to managed code.

  3. #3
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    Re: Why is Thread.Abort not doing anything?

    I expect MsgBox("Exception HIT!") to be reached ... but it is not ...
    you probably have not been patient enough. the message box is finally displayed when cmd.ExecuteNonQuery() returns. as the executed sql will not return for 1h, the cmd should timeout after the default 30 seconds which is when the messagebox gets displayed on my system.

    you can set the commandtimeout to 5 seconds for an earlier effect:
    Code:
                                                         Using cmd = con.CreateCommand()
                                                             cmd.CommandTimeout = 5
                                                             cmd.CommandText = "WHILE 0 = 0 WAITFOR DELAY '01:00:00'"
                                                             cmd.ExecuteNonQuery()
                                                         End Using

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width