Restarting Threads

  • Thread starter Thread starter Max
  • Start date Start date
M

Max

I have a small program which has a separate thread for the user
interface. Actually the interface is just used as an output for the
status of objects within the program. Anyway, this is basically the
whole new thread:


Private Sub Start()
ConsoleGUI.ShowDialog()
End Sub

When I call MyThread.Start() it runs that function and passes the
control over to the form. My problem is that once the form is closed the
thread obviously comes to an end and stops, but there may be times when
that form needs to be started back up again. When I run MyThread.Start()
again it gives me an error saying that the thread status is incorrect.
Any way I can start that thread up again?
 
Max said:
I have a small program which has a separate thread for the user
interface. Actually the interface is just used as an output for the
status of objects within the program. Anyway, this is basically the

whole new thread:


Private Sub Start()
ConsoleGUI.ShowDialog()
End Sub

When I call MyThread.Start() it runs that function and passes the
control over to the form. My problem is that once the form is closed
the thread obviously comes to an end and stops, but there may be
times when that form needs to be started back up again. When I run
MyThread.Start() again it gives me an error saying that the thread
status is incorrect. Any way I can start that thread up again?

No. Is there a reason not to create a new Thread and start it, or are you
just interested in the answer?


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Armin said:
No. Is there a reason not to create a new Thread and start it, or are you
just interested in the answer?

So would I have to do something like this then:

If MyThread.ThreadState = ThreadState.Unstarted Then
MyThread.Start()
Else
MyThread = New Thread(MyThreadStart)
MyThread.Start()
End If

?
 
Max said:
So would I have to do something like this then:

If MyThread.ThreadState = ThreadState.Unstarted Then
MyThread.Start()
Else
MyThread = New Thread(MyThreadStart)
MyThread.Start()
End If

?

Yes. I'd probably write:

If MyThread Is Nothing _
OrElse MyThread.ThreadState = ThreadState.Stopped Then

MyThread = New Thread(MyThreadStart)
End If

MyThread.Start()


What I usually do is raise an event at the end of the thread's main
procedure, and in the event handler, I set MyThread = Nothing, so I only
have to check whether MyThread is Nothing.

Of course, you must also take into account what to do if the thread is still
running, but how depends on the circumstances.

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Max,
I would probably use something like:
MyThread = New Thread(MyThreadStart)
MyThread.Start()

Assuming that only this routine started MyThread, I would only create the
thread when I needed to start it, this way checking to see if it was not
started is moot...

Hope this helps
Jay
 
Back
Top