Q: Advice on threads

  • Thread starter Thread starter G .Net
  • Start date Start date
G

G .Net

Hi Everybody

I was wondering if you experts could cast your eyes over the following code
and tell me if I'm doing anything obviously wrong. The code works but causes
some unexpected things in an application I'm writing e.g. a form I'm setting
to be hidden using .Hide is not hidden:
Private m_threadProgress As New Thread(AddressOf ProgressForm)

' the next few lines occur in a sub

If Me.m_threadProgress.IsAlive = False Then

Me.m_threadProgress = New Thread(AddressOf ProgressForm)

End If

Me.m_threadProgress.Start()

' do some other stuff

Me.m_threadProgress.Abort()

' display another form

Can you give me any advice as to whether I'm doing this correctly?

Thanks in advance

G
 
Can you give me any advice as to whether I'm doing this correctly?

Calling Thread.Abort is generally a bad idea. Other than that it's
hard to say anything without seeing more of your code. Are you
creating or interacting with any UI components from your background
thread?


Mattias
 
Hi Mattis

The thread displays a form with some flashing text. I'm using it to show
that long calculations are proceeding.

You say that Thread.Abort is a bad idea. How should I stop the thread and
dispose of it when it is no longer in use?

Thanks for your help.
 
In the form, create a boolean property to tell the form you are ready for it
to close. Then, where you use the thread.Abort(), simply set this property
to true. Inside the form's code, periodically check this flag and exit.

Mike Ober.

G .Net said:
Hi Mattis

The thread displays a form with some flashing text. I'm using it to show
that long calculations are proceeding.

You say that Thread.Abort is a bad idea. How should I stop the thread and
dispose of it when it is no longer in use?

Thanks for your help.
 
Do you by chance have a form that is opening as a showmodal form in a
different thread?

If so, could you lemmi know how u did that. Ive been trying to do something
simillar.

Miro

G .Net said:
Hi Mattis

The thread displays a form with some flashing text. I'm using it to show
that long calculations are proceeding.

You say that Thread.Abort is a bad idea. How should I stop the thread and
dispose of it when it is no longer in use?

Thanks for your help.
 
Hi Michael

How can I set the property of the form from the thread?

G

Michael D. Ober said:
In the form, create a boolean property to tell the form you are ready for
it to close. Then, where you use the thread.Abort(), simply set this
property to true. Inside the form's code, periodically check this flag
and exit.

Mike Ober.
 
Hi Miro

In the code I used above, simply have

Private Sub ProgressForm()
Dim form As MyForm = New MyForm()
form.ShowDialog()
End Sub

However, the problem is how do we close the form succesfully? From what I
gather, using Abort is not a good idea; although it does appear that the
form is closed in calling it.

G

Miro said:
Do you by chance have a form that is opening as a showmodal form in a
different thread?

If so, could you lemmi know how u did that. Ive been trying to do
something simillar.

Miro
 
Generally, I would have the thread do the work and have the flashing dialog
in my main UI thread. You can have the thread trigger an event that you
catch in the UI thread when the worker thread is done calculating then close
the dialog by calling a method from the finished event that the worker thread
fired.
 
Yeah Im trying something similar but once all my calculations are done, I
put a command button "close" that shows up that the user selects to
continue.

-User wanted to know it was complete instead of it dissapearing
automatically.

I will try to close it automatically and see what I find too. Ill be
hitting my code this weekend agian.

Miro
 
Back
Top