Help with starting my Windows Forms application properly :-)

S

Simon Harvey

Hi all,

I am trying to get my win forms application to start, but I'm hitting a
problem which I am certain is really simple.
The start for my application is ApplicationManager.cs. This class contains
the Main() method and a very simple constructor. The class looks something
like this:

public class{

[STAThread]
static void Main(){
ApplicationManager theApp = new ApplicationManager()
}

public ApplicationManager(){
...
}
}

The ApplicationManager class is hopefully going to be the central area to my
application. It is going to link the UI layer to the data layer. It's
basically going to be a class that coordinates everything.

At startup, one of the ApplicationManager's jobs is to start the user
interface. I've made another class called UIManager that the
ApplicationManager should interact with to do this.
The UIManager should be the only class that has direct access to the forms
defined in the application.

This is my problem (thanks for bearing with me!):

I don't know where to put the Application.Run() line. I want the UIManager
to create the main form of the user interface, however, if I put
Application.Run in the contructor of the UIManager - the application hangs:

private MainForm mf;

public UIManager (){
mf = new MainForm();
Application.Run(mf);

// Nothing will run beyond this point!
}

I know I have to use Application.Run method to get the Windows Message pump
'thingy' to work - but how can I start the message pump the way I want to,
without the application hanging?

Thanks thanks and more thanks to anyone who can help. This problem has been
bugging me on and off for ages now.

Simon
 
M

Mailing Lists

Assuming you want to use your existing design: If you put the
Application.Run in the UIManager constructor, it will block the constructor
causing the app to hang (as you see). Try adding a StartUI method to the
UIManager class that wraps this call instead. Then you could have the
ApplicationManager call the method after constructing the UIManager. All of
this assumes you want to use your existing classes.
 
S

Simon Harvey

Thanks for that.

I've just been sitting and thinking about this problem and I think I
understand the situation better now.

Am I right in thinking that essentially what I need to do is do all my
initialisation of non-ui stuff, before I do Application.Run()?

Before, I didnt understand why Application.Run() should block until the main
form was closed, as this would stop any code after Application.Run() from
being executed. I didnt understand why it worked like that and why that
should be allowed.

Now I am sort of imagining a situation in which I can do all sorts of
initialisation and whatever else I need to do as long as it's done before
Application.Run(). I am sort of imagining Application.Run() as a method that
puts the application into "UI Mode" at which point the application no longer
runs code in a predetermined sequence, but in an event driven manner.

Everything you want to do before the UI shows up, should be done before
switching into this "mode".

I'm sure my understanding is a bit rough, but could anyone tell me if what I
have said even makes the slightest bit of sense?!!

:)

Thanks all

Simon
 

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