Results 1 to 3 of 3

Thread: die thread die!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2006
    Posts
    69

    die thread die!

    Hello,
    i have a problem with thread.abort. i am using it instead of autoreset event because I have minimal cleanup and when it works it is a lot quicker. I have included the core bits that I am using. btnStartExecution creates the background thread and btnStopExecution is supposed to stop it. what i am trying to do is force a ThreadAbortException and then allow join to do the rest. Unfortunately I can't seem to get Thread.Abort to throw an exception and this is what I need help with. The problem is in the code below in abortThread method. I am cleaning up manually using a custom event run.Stopped, but still need the thread to die before carrying on. Any help appreciated.

    Code:
    Private Delegate Sub _StopExecution_delegate()
    Private Delegate Sub _abortThread_delegate()
        
    Private Sub btnStartExecution_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartExecution.Click
       '//m_thread is global thread
       m_Thread = New Thread(AddressOf StartExecution)
    
       With m_Thread
          .IsBackground = True
          .Start()
       End With
    End Sub
    
    Private Sub btnStopExecution_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopExecution.Click
        frmTreeViewInstance.Invoke(New _StopExecution_delegate(AddressOf StopExecution))
    End Sub
    
    Public Sub StopExecution()
       Dim strMessage As String = "Halting execution..."
       report.Write("StopExecution", strMessage, report.Warning, 1) '//custom reporting function
       run.RunStatus = run.Stopped '//handles minimal cleanup required.
     
       Dim del As _abortThread_delegate = New _abortThread_delegate(AddressOf abortThread)
       del.Invoke()
    End Sub
    
    Private Sub abortThread()
       Try
          If m_Thread.IsAlive Then
             m_Thread.Abort()
          End If
    
       Catch
    		  '//no ThreadAbortException ever caught?!?
       Finally
          Do Until m_Thread.Join(500) 
             Application.DoEvents()
          Loop
    
          Dim del As RunStateDelegate = New RunStateDelegate(AddressOf SetRunStateStopped)
          del.Invoke()
       End Try
    End Sub

  2. #2
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: die thread die!

    Hi,

    Here's a link about the Thread Abort method and the ThreadAbortException:

    http://msdn.microsoft.com/en-us/libr...64(VS.71).aspx

    It could help you,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  3. #3
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: die thread die!

    Aborting threads in general can be bad. While it's more work, I find that putting loop-breakers in my thread routine works best to kill them gracefully with minimal problems. Then, all you need is a Thread.Join()
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

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