Application.Run(Form) required?

  • Thread starter Thread starter Markus Schertler
  • Start date Start date
M

Markus Schertler

Hello,

i'm a bit confused. I don't have a main form in my
application.
I start with sub main. And simply had a Form.Show in it.
Now I read that application.Run schould be used. Why?

Can I simply show my form with Form.Show in
Sub Main and end my application with Unload Me in the form?
Or is it better to use Application.Run and
Application.Exit?

What code can I call after Application.Exit has executed?
Should this be the last statement I call? Can or should I
call Application.Exit even when I not called
Application.Run? Sorry. But I don't understand the meaning
and the difference of this. Up until now. I didn't use the
Application Object.

Thanks, Markus
 
Hello,
i'm a bit confused. I don't have a main form in my
application.
I start with sub main. And simply had a Form.Show in it.
Now I read that application.Run schould be used. Why?

Can I simply show my form with Form.Show in
Sub Main and end my application with Unload Me in the form?
Or is it better to use Application.Run and
Application.Exit?

you do not have to use Application.Run() method. instead you could for
example write:

using ( MyForm myForm = new MyForm() )
{
. . .
myForm.Show();
. . .
while ( myForm.Created )
{
...
Application.DoEvents();
}
}

as you can see the Application object is used here to handle Windows
messages.

Regards,
Wiktor Zychla
 
Hi Markus,
i'm a bit confused. I don't have a main form in my
application. I start with sub main. And simply had a Form.Show in it.
Now I read that application.Run schould be used. Why?
This might be different in VB (which could explain your confusion, VB dudes?) but in C# if you do not use Application.Run your form will display, but only briefly before the application exits i.e. if you do:
Form1 frm = new Form1() ;
frm.Show() ;

the code never enters a message loop. A message loop is broadly an loop which reads messages of the windows message queue, and dispatches them to your application, allowing you to respond to events such as a button click. Win32 programmers know and love them well, but they have been abstracted away by frameworks over the years.
If you call Application.Run() you spin up a message loop for your application. This means your window will stay up and responed to events until you Close it, or call Application.Exit. If you call Application.Run and pass it a Form, then the ApplicationContext (usually created in the Run method though you can create your own) notes that your window is the main window and terminates the message loop (i.e. exits Application.Run) if you close that window (if you do not override OnMainFormClosed).
Show is really just about visibility (though not ShowDialog, which does spin up a message loop for the dialog box, so you could have an application that just called ShowDialog()). In fact if you examine Form.Show with a decompiler like Reflector it just sets the Visible property of the form to true.
HTH,

Ian Cooper
wwww.dnug.org.uk
 
Hello,

Markus Schertler said:
i'm a bit confused. I don't have a main form in my
application.
I start with sub main. And simply had a Form.Show in it.
Now I read that application.Run schould be used. Why?

Your application needs a message loop. If you use Show in the Sub Main
without installing a message loop, your application will terminate.
Can I simply show my form with Form.Show in
Sub Main and end my application with Unload Me in
the form?

Unload Me doesn't work any more. You can use "Me.Close()".
Or is it better to use Application.Run and
Application.Exit?

It's better to use "Application.Run(New MainForm())" to start the form
and quit the application by closing windows.

Regards,
Herfried K. Wagner
 
Back
Top