Form.Show error within a Thread

J

Julia Chen

I have an application that has a thread which listens to a port for incoming
messages. The thread is declared and started in my main form, that is
opened from my sub main. In this thread, when a message has an ID in it, i
want a new form to show. I currently do this within the thread:

gContactForm = New frmContact(ID)
gContactForm.MdiParent = gMainForm.ActiveForm
gContactForm.Show()

But on the gContactForm.Show(), the form appears to 'hang' as a white
window.

Why is this happening, and more importantly, how can i fix it?
 
H

Herfried K. Wagner [MVP]

Julia Chen said:
I have an application that has a thread which listens to a port for
incoming messages. The thread is declared and started in my main form,
that is opened from my sub main. In this thread, when a message has an ID
in it, i want a new form to show. I currently do this within the thread:

Always create your forms in your application's main UI thread. Instance
members of Windows Forms forms and controls are not safe for multithreading,
so accessing them directly can cause problems. In addition to that, your
forms need a message pump which is normally provided by the app's main UI
thread.

Multithreading in Windows Forms applications
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=multithreading&lang=en>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top