Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,092 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,813 people online right now. Registration is fast and FREE... Join Now!




Thread Abort

 
Reply to this topicStart new topic

Thread Abort

anjali123
5 May, 2008 - 11:11 PM
Post #1

New D.I.C Head
*

Joined: 5 May, 2008
Posts: 1

vb

Public Sub CreateCase()
Dim xmlDoc As New XmlDocument
Dim xmlNode As XmlNode
Dim strSustainPath As String
Dim strRunMode As String

Try

g_blnTransaction = True
frmProgress1 = New frmProgress
ProgressbarThread = New System.Threading.Thread(AddressOf SetCaseProgressBar)
ProgressbarThread.SetApartmentState(ApartmentState.STA)
' Start the thread
ProgressbarThread.Start()

TrasactionThread = New System.Threading.Thread(New ThreadStart(AddressOf sustainCreateCase))
TrasactionThread.SetApartmentState(ApartmentState.STA)
TrasactionThread.IsBackground = True
TrasactionThread.Start()


g_blnTransaction = False
g_blnSave = True

Catch ex As Exception
g_blnTransaction = False
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
objLogger.WriteToLog("frmCase", "CreateCase", ex.Message, , mlLogger006.cLog.LogOptions.ShowMsgBox)
End Try
End Sub

Private Sub sustainCreateCase()
Try
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf sustainCreateCase))
Else
System.Windows.Forms.Application.DoEvents()
i = oCase.AddCase(strCourtKey, intErrorStatus, strErrorMessage)
If strErrorMessage.Trim <> "" Then
MessageBox.Show(strErrorMessage, "<app>")
If TrasactionThread.IsAlive Then
Try
TrasactionThread.Abort()
Catch ex As ThreadAbortException
Thread.ResetAbort()
Finally
' TrasactionThread.Join()
TrasactionThread = Nothing
End Try
End If
If ProgressbarThread.IsAlive Then
Try
ProgressbarThread.Abort()
Catch ex As ThreadAbortException
Thread.ResetAbort()
Finally
' ProgressbarThread.Join()
ProgressbarThread = Nothing
End Try
End If
Exit Sub
End If
If TrasactionThread.IsAlive Then
Try
TrasactionThread.Abort()
Catch ex As ThreadAbortException
Thread.ResetAbort()
Finally
' TrasactionThread.Join()
TrasactionThread = Nothing
End Try
End If
If ProgressbarThread.IsAlive Then
Try
ProgressbarThread.Abort()
Catch ex As ThreadAbortException
Thread.ResetAbort()
Finally
' ProgressbarThread.Join()
ProgressbarThread = Nothing
End Try
End If
End If
Catch ex As ThreadAbortException
Thread.ResetAbort()
End Try
End Sub

Private Sub SetCaseProgressBar()
Dim strProcedureName As String = "SetProgressBar"
Dim strTemp As String
Dim g As Graphics
Dim f As SizeF
Try
frmProgress1.blnDocument = True
frmProgress1.Text = "Creating Case . . ."
frmProgress1.lblStatus.Text = ""
strTemp = "Creating Case Number for " & txtCaseTitle.Text
g = Graphics.FromHwnd(frmProgress1.lblStatus.Handle)
f = g.MeasureString(strTemp, frmProgress1.lblStatus.Font)
frmProgress1.lblStatus.Width = Convert.ToInt16(f.Width) + 10
frmProgress1.Width = frmProgress1.lblStatus.Width + 15
frmProgress1.Height = frmProgress1.lblStatus.Top + 50
If frmProgress1.Width < frmProgress1.FileConfAnim.Width Then
frmProgress1.Width = frmProgress1.FileConfAnim.Width + 15
frmProgress1.FileConfAnim.Left = 8
Else
frmProgress1.FileConfAnim.Left = (frmProgress1.Width - frmProgress1.FileConfAnim.Width) / 2
End If
frmProgress1.lblStatus.Text = strTemp
frmProgress1.ShowDialog()

Catch ex As Exception
' MessageBox.Show(ex.Message)
'objLogger.WriteToLog(ModuleName, strProcedureName, "Exception in SetProgressBar procedure", xmlLogger003.cLog.LogOptions.NONE, "", LogLevel.High, "", "Exception Message" & ex.Message)
Finally

End Try
End Sub


In above code i am geting thread abort exception if i run above code recursively.

EDIT: Code blocks added => code.gif

This post has been edited by PsychoCoder: 6 May, 2008 - 05:01 AM
User is offlineProfile CardPM
+Quote Post

nealgabriel45
RE: Thread Abort
6 May, 2008 - 04:11 AM
Post #2

New D.I.C Head
Group Icon

Joined: 22 Oct, 2007
Posts: 46


Dream Kudos: 50
My Contributions
QUOTE(anjali123 @ 6 May, 2008 - 12:11 AM) *

CODE

Public Sub CreateCase()
        Dim xmlDoc As New XmlDocument
        Dim xmlNode As XmlNode
        Dim strSustainPath As String
        Dim strRunMode As String

        Try
                                      
            g_blnTransaction = True
             frmProgress1 = New frmProgress
            ProgressbarThread = New System.Threading.Thread(AddressOf SetCaseProgressBar)
            ProgressbarThread.SetApartmentState(ApartmentState.STA)
            ' Start the thread
            ProgressbarThread.Start()

            TrasactionThread = New System.Threading.Thread(New ThreadStart(AddressOf sustainCreateCase))
            TrasactionThread.SetApartmentState(ApartmentState.STA)
            TrasactionThread.IsBackground = True
            TrasactionThread.Start()
          

            g_blnTransaction = False
            g_blnSave = True

        Catch ex As Exception
            g_blnTransaction = False
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
            objLogger.WriteToLog("frmCase", "CreateCase", ex.Message, , mlLogger006.cLog.LogOptions.ShowMsgBox)
        End Try
    End Sub

Private Sub sustainCreateCase()
                Try
            If Me.InvokeRequired Then
                Me.Invoke(New MethodInvoker(AddressOf sustainCreateCase))
            Else
                System.Windows.Forms.Application.DoEvents()
                i = oCase.AddCase(strCourtKey, intErrorStatus, strErrorMessage)
               If strErrorMessage.Trim <> "" Then
                    MessageBox.Show(strErrorMessage, "<app>")
                    If TrasactionThread.IsAlive Then
                        Try
                            TrasactionThread.Abort()
                        Catch ex As ThreadAbortException
                            Thread.ResetAbort()
                        Finally
                            ' TrasactionThread.Join()
                            TrasactionThread = Nothing
                        End Try
                    End If
                    If ProgressbarThread.IsAlive Then
                        Try
                            ProgressbarThread.Abort()
                        Catch ex As ThreadAbortException
                            Thread.ResetAbort()
                        Finally
                            '  ProgressbarThread.Join()
                            ProgressbarThread = Nothing
                        End Try
                    End If
                    Exit Sub
                End If
                If TrasactionThread.IsAlive Then
                    Try
                        TrasactionThread.Abort()
                    Catch ex As ThreadAbortException
                        Thread.ResetAbort()
                    Finally
                        ' TrasactionThread.Join()
                        TrasactionThread = Nothing
                    End Try
                End If
                If ProgressbarThread.IsAlive Then
                    Try
                        ProgressbarThread.Abort()
                    Catch ex As ThreadAbortException
                        Thread.ResetAbort()
                    Finally
                        ' ProgressbarThread.Join()
                        ProgressbarThread = Nothing
                    End Try
                End If
                End If
        Catch ex As ThreadAbortException
            Thread.ResetAbort()
        End Try
    End Sub

    Private Sub SetCaseProgressBar()
        Dim strProcedureName As String = "SetProgressBar"
        Dim strTemp As String
        Dim g As Graphics
        Dim f As SizeF
        Try
            frmProgress1.blnDocument = True
            frmProgress1.Text = "Creating Case . . ."
            frmProgress1.lblStatus.Text = ""
            strTemp = "Creating Case Number for " & txtCaseTitle.Text
            g = Graphics.FromHwnd(frmProgress1.lblStatus.Handle)
            f = g.MeasureString(strTemp, frmProgress1.lblStatus.Font)
            frmProgress1.lblStatus.Width = Convert.ToInt16(f.Width) + 10
            frmProgress1.Width = frmProgress1.lblStatus.Width + 15
            frmProgress1.Height = frmProgress1.lblStatus.Top + 50
            If frmProgress1.Width < frmProgress1.FileConfAnim.Width Then
                frmProgress1.Width = frmProgress1.FileConfAnim.Width + 15
                frmProgress1.FileConfAnim.Left = 8
            Else
                frmProgress1.FileConfAnim.Left = (frmProgress1.Width - frmProgress1.FileConfAnim.Width) / 2
            End If
            frmProgress1.lblStatus.Text = strTemp
            frmProgress1.ShowDialog()

        Catch ex As Exception
            '  MessageBox.Show(ex.Message)
            'objLogger.WriteToLog(ModuleName, strProcedureName, "Exception in SetProgressBar procedure", xmlLogger003.cLog.LogOptions.NONE, "", LogLevel.High, "", "Exception Message" & ex.Message)
        Finally

        End Try
    End Sub


In above code i am geting thread abort exception if i run above code recursively.



User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Thread Abort
6 May, 2008 - 05:03 AM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Anytime you use Thread.Abort() you will get that exception, it's by design. Whenever you use that, put it inside a Try..Catch block and catch a ThreadAbortException, then do nothing


vb

Try

Catch(ex As ThreadAbortException)
End Try

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:03AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month