multithreading problem in winforms

G

Guest

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(AddressOf ShowfrmWorking)
workingThread = New System.Threading.Thread(WorkingStart)

workingThread.SetApartmentState(System.Threading.ApartmentState.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
 
R

rowe_newsgroups

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top