Yea something strange is going on, for example, if the first thread
finishes before the form is closed the form also seems to freeze for
some reason. I have to suspend the main thread just before it ends and
then resume it at the end of the second thread. The way my program runs
is, I have some class A and a form B, inside class A I define a new
instance of B, something like Private MyForm as New B. Sub Main creates
a new instance of A with Dim MyClass as New A, and then I make the call
MyClass.OpenForm. The idea is to have the form opened and allow the user
to interact with it, but let the program execution continue and not
close the form even if it reaches the end. So my program ends only when
both the main thread and the GUIThread have ended.
Do you notice anything wrong with what I’m doing?
AlexS wrote:
Max,
something is not right here. Your ShowDialog is static (shared)? Or MyForm
is instance reference?
Where it was instantiated?
I used successfully similar technique, but form was instantiated ( = new
MyForm) in the thread too. And no sleep was required. It could be necessary
only if after OpenForm thread goes into tight loop and doesn't allow other
threads to run.
HTH
Alex
"Max" <ma*****@yahoo.com> wrote in message
news:zo********************@comcast.com...
Playing around with multi-threading programs and ran into this little
problem that maybe someone here could explain... Basically I have a
class which launches a form object. Once the form is launched (using
ShowDialog) I still needed both to be available, so my solution was to
start the form in a new thread. This way the program itself can still
use the class and the user can interact with the form. So here's how I
did this:
Public Sub OpenForm()
GUIThread = New Thread(GUIThreadStart)
GUIThread.Start()
Thread.CurrentThread.Sleep(1000)
End Sub
Private Sub MyThread()
MyForm.ShowDialog()
End Sub
I didn't put in all the definitions, but basically MyThread() is the
starting point for the GUIThread. The problem comes in where you see
that Sleep(1000) line. If I don't put that line in the form opens up
completely unresponsive and essentially looks frozen. Why does that
happen? I thought as soon as I start the new thread it just goes along
its own road. Why do I need to pause the execution of the main thread in
order to have the form be able to show?