Home All Groups Group Topic Archive Search About

system.threading - VB

Author
25 Jan 2006 6:07 AM
Roger
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

Author
26 Jan 2006 8:24 AM
Dmytro Lapshyn [MVP]
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).

Show quote
"Roger" <Ro***@discussions.microsoft.com> wrote in message
news:152ADC28-3642-4AF0-93B5-115B6E20C438@microsoft.com...
>
> 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
Author
27 Jan 2006 3:08 AM
Roger
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.
--
Roger


Show quote
"Dmytro Lapshyn [MVP]" wrote:

> 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).
>
> "Roger" <Ro***@discussions.microsoft.com> wrote in message
> news:152ADC28-3642-4AF0-93B5-115B6E20C438@microsoft.com...
> >
> > 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
>
>
>

AddThis Social Bookmark Button