system.threading - VB

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

First time with threads.

I have a simple but vexing problem.
There are two Win Forms FormA and FormB.
FormA is shown dialog on the main thread.
When the user clicks button1 on FormA a new workerthread is started if the
worker
thread is not alive.
If the worker thread is alive nothing happens.

The problem is when FormA is closed and FormB is alive I get an unhandled
exception at run time ... 'thread was being aborted'
In VS debugger the code works fine without errors.


The threadstart delegate looks like
Public Sub begin_formB()
dim newB as formB
Try
newB = New FormB
newB.ShowDialog()
newB.Dispose()
Catch e As ThreadAbortException
newB.close
newB.Dispose()
Finally
newB.close
newB.Dispose()
End Try
End Sub

The button1.click event handler looks like
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
' note tr is declared as a public thread variable in sub main
If tr.IsAlive Then Then Exit Sub
tr.Start()
End Sub

Sub main looks like
Public tr As New Thread(New ThreadStart(AddressOf results.begin_formB))
Sub Main()
My.Forms.FormA.ShowDialog()
If tr.IsAlive Then
tr.Abort()
tr.Join()
End If
End Sub

I can't understand why a runtime unhandled exception error of 'thread was
being aborted' .
I think my threadstart delegate is incorrect..
Any help?

Roger
 
Hi Roger,

You should never do any UI on worker threads. Re-design your app that both
forms are shown from the main thread and probably use asynchronous delegates
to perform lengthy tasks without blocking the UI.

Also consider modeless dialog window mode for Form B (I might be right in
guessing what you are actually trying to achieve).
 
Hi Dmytro,
Thanks for your help.
I will try as you suggested.
In the meantime I have found a solution that works. Which is to call the
worker thread.abort method in the form.closing event of the main form.
I believe the previous error was caused by the main form closing before the
worker thread was destroyed.
Actually I now have a situation with four forms working on four separate
threads.
All I do is in the main formclosing event I call thread.abort , thread.join
on each worker thread. The main thread is put to sleep for 50msecs after each
thread.abort call. This provides sufficent time to allow proper exiting of
the app.

My question is will this methodology provide consistent long term results.

It is nice to have the four forms on separate threads providing user
responsiveness on each form while the user can compare/massage data from
form to form.
Relevant data is passed between threads.

I believe if all forms were on the main thread the user would have to close
all visible sub forms to do any work on the main form. Or am I missing
somehing about Win Forms?
When finished the app will have about 20 forms.
There are 4 important forms that I was going to operate on worker threads.
The other 16 including the main form will operate on the main thread.
Is this feasible?
Thanks in advance.
 
Back
Top