A question about Application.Run()

  • Thread starter Thread starter Simon Harvey
  • Start date Start date
S

Simon Harvey

Hi everyone,

Can anyone tell me if there is any compelling reason not to start my
application in a way other than :

Application.Run(new MyForm());

I'm not too sure of the implications of scraping that approach and doing :

Myform mForm = new MyForm();
mForm.Visible = true;

I'm having a little problem, which at first glance would seem to be fixable
by using the later method as opposed to the former.

I'm not to sure of the implications of not using the Application.Run
mechanism though. As I understand it, it manages windows messages and starts
a thread or something. That all sounds jolly important so do I want to mess
with it?

Many thanks to anyone who can advise

Kind Regards

Simon
 
Simon said:
Hi everyone,

Can anyone tell me if there is any compelling reason not to start my
application in a way other than :

Application.Run(new MyForm());

If you write a console application, then you will not call
"Application.Run",.

I'm not too sure of the implications of scraping that approach and
doing :

Myform mForm = new MyForm();
mForm.Visible = true;

I'm having a little problem, which at first glance would seem to be
fixable by using the later method as opposed to the former.

I'm not to sure of the implications of not using the Application.Run
mechanism though. As I understand it, it manages windows messages and
starts a thread or something. That all sounds jolly important so do I
want to mess with it?


"Application.Run" runs the message pump... and only exits the program if
"Close" is selected.


If you do only:

void Main()
{
Myform mForm = new MyForm();
mForm.Visible = true;
}

then your app is immediately exiting... because if only céxecutes the
constructor, sets the property "Visible" to true and the exists...
No one who paints the window... and runs the message pump...



--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
 
Thank you Jochen,

So are you seeing that I really must use the Application.Run method?

I just tried to put that into another class but it wouldnt work. It only
seems to work in the Main method of the application. Is that normal? I made
sure the right imports were there so I'm not sure why it didnt work

Thanks again
Simon
 
Simon,

You could put it in another class, but you should only have one message
pump running in a thread. Having two is not a good idea (as the outer pump
waits until the inner pump is finished processing).

Hope this helps.
 
You could put it in another class, but you should only have one
message
pump running in a thread. Having two is not a good idea (as the outer pump
waits until the inner pump is finished processing).

Hope this helps.


Thanks Nicholas,

I have a class called CommsApplication.cs. It's just a small play around
application for learning about this sort of stuff.

In the Main method I have:

[STAThread]
static void Main() {
Application.Run(new MainForm());
new CommsApp();
}


As you'll probably know already, this doesnt have the effect that I desire.
What I want to happen is for the CommsApp classes constructor to be called.
But the Application.Run() method is stopping this from happening. I think
its because the main thread is hogging all the processor time. The CommsApp
constructor does actually run, but only after I have closed the form! I can
sort of see why though.

What would the best way to get the CommsApp class constructor invoked. I
could put the Application.Run() line at the end of the Main() method. Do you
know if that would be a good idea.

Thanks very much for your help

:-)

Simon
 
Simon,

You have a number of ways you can do this. The first would be to have
the MainForm constructor call the constructor for the CommsApp class and
store the reference in the instance. The second would be to modify the
constructor of the MainForm class to take an instance of the CommsApp class
and pass it into the constructor for MainForm. Then, you would create the
class before the call to Application.Run.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Simon Harvey said:
You could put it in another class, but you should only have one message
pump running in a thread. Having two is not a good idea (as the outer pump
waits until the inner pump is finished processing).

Hope this helps.


Thanks Nicholas,

I have a class called CommsApplication.cs. It's just a small play around
application for learning about this sort of stuff.

In the Main method I have:

[STAThread]
static void Main() {
Application.Run(new MainForm());
new CommsApp();
}


As you'll probably know already, this doesnt have the effect that I desire.
What I want to happen is for the CommsApp classes constructor to be called.
But the Application.Run() method is stopping this from happening. I think
its because the main thread is hogging all the processor time. The CommsApp
constructor does actually run, but only after I have closed the form! I can
sort of see why though.

What would the best way to get the CommsApp class constructor invoked. I
could put the Application.Run() line at the end of the Main() method. Do you
know if that would be a good idea.

Thanks very much for your help

:-)

Simon
 
[STAThread]
static void Main()
{
Form f = new MainForm();
Object comm = new CommsApp();
Application.Run(f);
}

Nicholas Paldino said:
Simon,

You have a number of ways you can do this. The first would be to have
the MainForm constructor call the constructor for the CommsApp class and
store the reference in the instance. The second would be to modify the
constructor of the MainForm class to take an instance of the CommsApp class
and pass it into the constructor for MainForm. Then, you would create the
class before the call to Application.Run.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

You could put it in another class, but you should only have one message
pump running in a thread. Having two is not a good idea (as the outer pump
waits until the inner pump is finished processing).

Hope this helps.


Thanks Nicholas,

I have a class called CommsApplication.cs. It's just a small play around
application for learning about this sort of stuff.

In the Main method I have:

[STAThread]
static void Main() {
Application.Run(new MainForm());
new CommsApp();
}


As you'll probably know already, this doesnt have the effect that I desire.
What I want to happen is for the CommsApp classes constructor to be called.
But the Application.Run() method is stopping this from happening. I think
its because the main thread is hogging all the processor time. The CommsApp
constructor does actually run, but only after I have closed the form! I can
sort of see why though.

What would the best way to get the CommsApp class constructor invoked. I
could put the Application.Run() line at the end of the Main() method. Do you
know if that would be a good idea.

Thanks very much for your help

:-)

Simon
 
Nice...
news.microsoft.com said:
[STAThread]
static void Main()
{
Form f = new MainForm();
Object comm = new CommsApp();
Application.Run(f);
}

message news:#[email protected]...
Simon,

You have a number of ways you can do this. The first would be to have
the MainForm constructor call the constructor for the CommsApp class and
store the reference in the instance. The second would be to modify the
constructor of the MainForm class to take an instance of the CommsApp class
and pass it into the constructor for MainForm. Then, you would create the
class before the call to Application.Run.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

You could put it in another class, but you should only have one
message
pump running in a thread. Having two is not a good idea (as the outer
pump
waits until the inner pump is finished processing).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Thanks Nicholas,

I have a class called CommsApplication.cs. It's just a small play around
application for learning about this sort of stuff.

In the Main method I have:

[STAThread]
static void Main() {
Application.Run(new MainForm());
new CommsApp();
}


As you'll probably know already, this doesnt have the effect that I desire.
What I want to happen is for the CommsApp classes constructor to be called.
But the Application.Run() method is stopping this from happening. I think
its because the main thread is hogging all the processor time. The CommsApp
constructor does actually run, but only after I have closed the form!
I
can
sort of see why though.

What would the best way to get the CommsApp class constructor invoked. I
could put the Application.Run() line at the end of the Main() method.
Do
you
know if that would be a good idea.

Thanks very much for your help

:-)

Simon
 
news.microsoft.com said:
[STAThread]
static void Main()
{
Form f = new MainForm();
Object comm = new CommsApp();
Application.Run(f);
}


Thanks everyone!

I was begining to think along the lines of the suggestion above. Hopefully I
won't need any more help!

Fingers crossed and thanks again for the advice

Simon
 
Back
Top