By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,377 Members | 1,655 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,377 IT Pros & Developers. It's quick & easy.

multithreading problem in winforms

P: n/a
Dear ng,

i have developed a winforms application with vs2005.
An progress from with a animation and a timer is shown while the
application is working.

The trouble is, that often a ThreadAbordException occurs and i can not
identify
whats the reason is.

Can somebody help, please?
Thank you very much
Michael

--##################################################
following code i use:
--##################################
Private Sub tspProgress_VisibleChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles tspProgress.VisibleChanged
Try
If tspProgress.Visible = True Then
Me.Enabled = False
Startzeit = Now
Dim WorkingStart As
System.Threading.ParameterizedThreadStart = New
System.Threading.ParameterizedThreadStart(AddressO f ShowfrmWorking)
workingThread = New System.Threading.Thread(WorkingStart)

workingThread.SetApartmentState(System.Threading.A partmentState.STA)
workingThread.Start()
Else
Me.Enabled = True
If (Not workingThread Is Nothing AndAlso
workingThread.IsAlive) Then
workingThread.Abort()
End If
While (workingThread.IsAlive)
Application.DoEvents()
End While
End If
Catch ex As Threading.ThreadAbortException
ErrorLog(ex)
Catch ex As Exception
ErrorLog(ex)
End Try
End Sub

--##################################
Public Sub ShowfrmWorking(ByVal Obj As Object)
Try
fWorking = New frmWorking
fWorking.ShowDialog()
Catch ex As Exception

End Try
End Sub
--############################################
Das anzuzeigende Form:
--############################################

Public Class frmWorking
Inherits System.Windows.Forms.Form
Dim Startzeit As DateTime

Private Sub frmWorking_HandleDestroyed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.HandleDestroyed
Try
Timer.Enabled = False
Catch ex As Threading.ThreadAbortException
ErrorLog(ex)
Catch ex As Exception
ErrorLog(ex)

End Try
End Sub

Private Sub frmWorking_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Try
Startzeit = Now
Timer.Enabled = True
Catch ex As Threading.ThreadAbortException
ErrorLog(ex)

Catch ex As Exception
ErrorLog(ex)
End Try
End Sub

Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer.Tick
Try
Me.Label1.Text = Format(DateAdd(DateInterval.Second,
DateDiff(DateInterval.Second, Startzeit, Now), New DateTime(Now.Year,
Now.Month, Now.Day, 0, 0, 0)), "00:mm:ss")
Me.Label1.Refresh()
Me.Refresh()
Catch ex As Threading.ThreadAbortException
ErrorLog(ex)

Catch ex As Exception
ErrorLog(ex)

End Try
End Sub
End Class

Sep 21 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
What is the purpose of the below code? Immediately after you start the
thread you are aborting it?

/////////////////
If (Not workingThread Is Nothing AndAlso workingThread.IsAlive) Then
workingThread.Abort()
End If
////////////////

I also think the below is redundant, if you spawn a seperate thread
you shouldn't need to call DoEvents:

//////////////
While (workingThread.IsAlive)
Application.DoEvents()
End While
//////////////

Thanks,

Seth Rowe

Sep 21 '07 #2

P: n/a
Calling Thread.Abort will cause a ThreadAbortException.

http://msdn2.microsoft.com/en-us/lib...exception.aspx
Michael W. wrote:
Dear ng,

i have developed a winforms application with vs2005.
An progress from with a animation and a timer is shown while the
application is working.

The trouble is, that often a ThreadAbordException occurs and i can not
identify
whats the reason is.

Can somebody help, please?
Thank you very much
Michael

--##################################################
following code i use:
--##################################
Private Sub tspProgress_VisibleChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles tspProgress.VisibleChanged
Try
If tspProgress.Visible = True Then
Me.Enabled = False
Startzeit = Now
Dim WorkingStart As
System.Threading.ParameterizedThreadStart = New
System.Threading.ParameterizedThreadStart(AddressO f ShowfrmWorking)
workingThread = New System.Threading.Thread(WorkingStart)

workingThread.SetApartmentState(System.Threading.A partmentState.STA)
workingThread.Start()
Else
Me.Enabled = True
If (Not workingThread Is Nothing AndAlso
workingThread.IsAlive) Then
workingThread.Abort()
End If
While (workingThread.IsAlive)
Application.DoEvents()
End While
End If
Catch ex As Threading.ThreadAbortException
ErrorLog(ex)
Catch ex As Exception
ErrorLog(ex)
End Try
End Sub

--##################################
Public Sub ShowfrmWorking(ByVal Obj As Object)
Try
fWorking = New frmWorking
fWorking.ShowDialog()
Catch ex As Exception

End Try
End Sub
--############################################
Das anzuzeigende Form:
--############################################

Public Class frmWorking
Inherits System.Windows.Forms.Form
Dim Startzeit As DateTime

Private Sub frmWorking_HandleDestroyed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.HandleDestroyed
Try
Timer.Enabled = False
Catch ex As Threading.ThreadAbortException
ErrorLog(ex)
Catch ex As Exception
ErrorLog(ex)

End Try
End Sub

Private Sub frmWorking_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Try
Startzeit = Now
Timer.Enabled = True
Catch ex As Threading.ThreadAbortException
ErrorLog(ex)

Catch ex As Exception
ErrorLog(ex)
End Try
End Sub

Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer.Tick
Try
Me.Label1.Text = Format(DateAdd(DateInterval.Second,
DateDiff(DateInterval.Second, Startzeit, Now), New DateTime(Now.Year,
Now.Month, Now.Day, 0, 0, 0)), "00:mm:ss")
Me.Label1.Refresh()
Me.Refresh()
Catch ex As Threading.ThreadAbortException
ErrorLog(ex)

Catch ex As Exception
ErrorLog(ex)

End Try
End Sub
End Class
Sep 21 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.