Re: Application.Run() Application.Exit and the 'Message Pump'
- From: "Dave" <NOSPAM-dave@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 1 May 2005 19:24:14 -0400
Hi Bill,
1)
I can't say it for sure that .NET is meant to support multiple Application.Run calls in-line. It seems to work fine, but probably
isn't the best design pattern for an application anyway. You can start a message loop on one form, and have that form "control" the
application and maintain state. It can then Hide itself and show a different Form if necessary.
FYI, you don't have to actually write this.Close in the form... it does this for you when you click the "X" or use any system
command that terminates an app. I'm just saying in case you weren't aware of this since it's an important piece of info.
2)
You have explained this correctly. It seems that Application.Exit attempts to invalidate the Application class and it's state to
ensure that the message loop will be terminated. It doesn't seem to care that you may want to use it again. Like I said in the
first answer, it may not be appropriate to start another message pump in most real-world scenerios.
3)
Good question, lol. I'd imagine that this would have the same effect as in 2, as you have, but apparently it doesn't.
I guess that when you show the first form, it's creating that *temp* pump in order to actually allow interaction with the form, but
it doesn't effect the state of the Application class. When you then call Application.Run, you aren't giving it a form, so maybe
it's just syncing-up with the previously started loop. I guess the state of the Application class is different than it is in 2,
which is why you can start another loop even after calling Application.Exit.
Summary)
Understanding the Application class and how it can be used successively to start message loops is one thing, but actually using this
logic in a real-world app is another. I think most end-users expect a single main window to open when they start an app. When they
close the app, they expect it to terminate. Opening another form and starting another app is not intuitive in the Windows
environment and so I would have to recommend that this be avoided unless absolutely necessary.
Is there a particular design that you're after or are you just curious about the Application class functionality?
--
Dave Sexton
dave@xxxxxxxxxxxxxxxxxxx
-----------------------------------------------------------------------
<orekinbck@xxxxxxxxxxxx> wrote in message news:1114943031.081026.313390@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hi Dave
>
> Thanks for your response - apologies for taking so long to reply, I
> decided to take a detour to Threading, as I thought that might help me
> with this topic.
>
> With your response and my detour to Threading, I'm more comfortable,
> I just have 3 more 'itty bitty' questions that I hope will clear
> the confusion I have over Application.Exit() ....
>
> As before, set up a project with one form, and that form just has a
> close button. In the close button's 'on click' event handler,
> just type:
> this.close();
>
> Add a class to the project and put in the start routine:
>
> public class StartUp
> {
> static void Main()
> {
> //This should 'block' until the user clicks the close button
> in the form
> Application.Run(new ExForm());
> //Should this second call to Application.Run work ? It does!
> Application.Run(new ExForm());
> Debug.WriteLine("Finished Main");
> }
> }
>
> At this point the application should consist of a form - ExForm, and
> a StartUp class.
>
> (1)
>
> In the above example, you should find that a form shows, and code
> execution is "blocked" until you click close. After clicking
> close, a second form shows, and code execution 'blocks' until you
> click close. Then finally, the application terminates. Confirmation
> Question - The closing of the first form does not 'mortally
> wound' the message pump and it can get started again for the second
> call to Application.Run?
>
> (2)
>
> Keep everything the same, but in the form, change the close button's
> 'on click' event handler to:
> Application.Exit();
>
> In this example, you should find that the first form shows, and code
> execution is "blocked" until you click close. After clicking
> close, a second form "flashes" on and off the screen. Confirmation
> Question - The closing of the first form causes
> 'Application.Exit()' which seems to 'mortally wound' the
> message pump. When the second call to Application.Run occurs, the
> message pump cannot be revived, and therefore code execution cannot be
> blocked. Execution falls through to the debug.WriteLine and then the
> end of static void Main is reached, causing the application to
> terminate.
>
> (3)
>
> With the close button's 'on click' event handler still calling
> 'Application.Exit', put this code into the StartUp class:
>
> public class StartUp
> {
> static void Main()
> {
> ExForm myExForm = new ExForm();
> myExForm.Show();
> Application.Run(); //This call to Application.Run should block
> //Should this second call to Application.Run work ? It does!
> Application.Run(new ExForm());
> Debug.WriteLine("Finished Main");
> }
> }
>
> In this above example, you should find that a form shows, and code
> execution is "blocked" until you click close. After clicking
> close, a second form shows, and code execution 'blocks' until you
> click close. Then finally, the application terminates.
>
> Unlike scenario (2), in this scenario the message pump is not
> 'mortally wounded ???? Is this something to do with the fact that we
> show the form then call Application.Run() with no parameters ???? Que
> !?!
>
> Cheers
> Bill
>
.
- Follow-Ups:
- Re: Application.Run() Application.Exit and the 'Message Pump'
- From: orekinbck
- Re: Application.Run() Application.Exit and the 'Message Pump'
- References:
- Re: Application.Run() Application.Exit and the 'Message Pump'
- From: orekinbck
- Re: Application.Run() Application.Exit and the 'Message Pump'
- Prev by Date: Re: Dynamic Buttons
- Next by Date: Abstract class or interface?
- Previous by thread: Re: Application.Run() Application.Exit and the 'Message Pump'
- Next by thread: Re: Application.Run() Application.Exit and the 'Message Pump'
- Index(es):
Relevant Pages
|