Can I create another form in another thread?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi, everybody

I want to create another form in another thread. But if I do this, the form will be created and closed immediately
Can I create another form in another thread

The purpose I want to do is that before the application is started, I want to display a starting form. So I create
the starting form in another thread in the form() handler. Or can I use another method to achieve this

thanks
 
Hi James -

You write: "before the application is started, I want to display a starting
form". You need to start an application in order to display anything. ;-)

It seems to me that you should simply change the first form to this starting
form.

In general I think its a bad idea to introduce another thread in your
program simply to create a form. Why don't you simply close/hide the first
form before opening the next? Pseudo code:

class MyForm : Form {
....
public void button_click() {
this.Hide();
MyOtherForm form = new MyOtherForm();
form.Show();
}
}

Can I create another form in another thread?

I don't know if there is a special GUI thread in .NET, which you are
required to use, but its a general principle in .NET winforms programming
that you NEVER modify the state of a control from a thread different from
the one responsible for creating it.

Spawning a new thread and creating a form in this thread shouldn't violate
that prinicple. There is a potential problem however, when your new form is
displayed and an event is triggered. It could be that the event is delivered
on a thread different from the one that created the form, thus creating some
problem causing the form to be closed.

Hope this helps

Kind regards
Thomas



james ou said:
hi, everybody,

I want to create another form in another thread. But if I do this, the
form will be created and closed immediately.
Can I create another form in another thread?

The purpose I want to do is that before the application is started, I want
to display a starting form. So I create
the starting form in another thread in the form() handler. Or can I use
another method to achieve this?
 
Hi again - I just noticed that the System.Windows.Forms.Application class is
used to launch the thread responsible for handling messages etc.

If you still want to use two threads you should try that class.

Thomas
 
Hi james,
If you want to open a form in a new thread you have to make that thread UI.
That is to run a message loop on it.
All you have to do is to call Application.Run in the new thread. You may
pass the form you want to show as a parameter to the Application.Run method.
Don't forget that the form has to be created by this new thread.

--
HTH
B\rgds
100

james ou said:
hi, everybody,

I want to create another form in another thread. But if I do this, the
form will be created and closed immediately.
Can I create another form in another thread?

The purpose I want to do is that before the application is started, I want
to display a starting form. So I create
the starting form in another thread in the form() handler. Or can I use
another method to achieve this?
 
Back
Top