Multi-threading causes problems

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

Max

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?
 
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
 
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?
 
Hi Max,

There is no need in my opinion for multithreading, as long as the user is
not handling form1 it does nothing untill it get a message that a user has
something done on that form and than an event will be raised.

However with the form.showdialog that is not even the situation, because
than your main form is disabled and will be enabled back when the showform
gives a message back (which is standard when the dialogform closes).

I hope this helps?

Cor
 
Max said:
<snip>... 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.

I don't see where and how you start main message pump for application. There
should be somewhere Application.Run call. Additionally, you should create
form on UI thread. I think it is due to how dialog messages are dispatched
by main message loop and which controls are on threaded form. Attach main
form event handler to Closing event of threaded form and then you will be
notified when form is closed. You can then Sleep or Join to wait until
threaded form is closed - Join would be better.

HTH now
Alex
 
Back
Top