Open form in a seperate thread?

D

Doezel

Hello,

I have a mainform. This mainform opens another form which starts a thread
for serial communication.

No I want to open antother from from my mainform (as the mainform is my
login screen).

I open it like this:
frmMainMenu MM=new frmMainMenu();

MM.ShowDialog();

But this causes my communication thread to stop until I close the MainMenu
form again.

My first thought was to open the MainMenu form in another thread.

1) will this keep my serial communication thread running?

2) how do I open a form in a thread?

Thanks,

Thijs.
 
D

Daniel Moth

Do not open other forms on secondary threads. All UI elements should be
running on the same thread.

If you want to have more than one form open and none of them blocking each
other, use Show rather than ShowDialog.

Cheers
Daniel
 
D

Doezel

Do not open other forms on secondary threads. All UI elements should be
running on the same thread.

If you want to have more than one form open and none of them blocking each
other, use Show rather than ShowDialog.

Thanks Daniel,

The nice thing of ShowDialog is that I immediately know when the called form
is closed, so I can start the logging out process, for example.

Is there a reason why all UI elements should run in one thread?

And why is my serial communication thread blocked when I open another form?
I thought that if I create a thread, it keeps on running, regardless of what
other forms I open.

I think I'll just have to use Show and Hide, instead of if
(form.ShowDialog==DialogResult...)

can this done like:

MainMenu.Show();
while (!MainMenu.boolHide) {}
//continue the rest of my application...

or would this aswell block my thread?

Thanks,

Thijs.
 
D

Daniel Moth

I don't know why your serial class is blocked without looking at some
running code.

The "all forms on same thread" is a windows rule.

Do not use the "while" approach! That will render your UI frozen. (by the
way, rather than asking this you could have tried it, observed the results
and learned from it).

If you want to know when another form closes, add an eventhandler to its
Closing event and in your eventhandler method run whtever code you wish.

Cheers
Daniel
 
D

Doezel

I don't know why your serial class is blocked without looking at some
running code.

Are you willing to look at an example?
The "all forms on same thread" is a windows rule.

To which I can relate, because that is what ShowDialog Does, though I didn't
expect that it would halt a thread nut using any UI.
Do not use the "while" approach! That will render your UI frozen. (by the
way, rather than asking this you could have tried it, observed the results
and learned from it).

I know while(){} is really nasty, but it would be a quick and dirty way to
solve my problem... :)
If you want to know when another form closes, add an eventhandler to its
Closing event and in your eventhandler method run whtever code you wish.

That's a good idea, thanks.
 

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