Determining Application Startup Form at run time

Anybody know of a pattern for determining the startup form of a .NET application at runtime Since the startup Main method is static, Template Method and other inheritance based patterns are problematic. Have not found any answers via Google either. I've seen the example using a derived class from the ApplicationContext class that allows setting of the MainForm property but that still leaves the "chicken and egg" problem of starting any kind of dynamic mechanism from a static method. Every scenario I've come up with seems to boil down to having to know some concrete class (builder, factory, etc) or the concrete form itself up front.

Any thoughts/recommendations or better yet, solutions



Answer this question

Determining Application Startup Form at run time

  • jh55557777

    Since I ould not ffigure out a way to make it work, I figured it was gonna end here. Thanks Common et. al. for all the help.
  • Erik Campo

    Yeah this is real easy to do. Check out the ApplicationContext class: http://msdn2.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx

     

    EDIT: Sorry - I didn't read your post enough the first time to notice that you know about the AppContext class. I don't understand what your needs are if that class doesn't work for you. An assembly when built will have a specific number of classes that derive from Form, so how "dynamic" do you need it to be

     

    That said your application will eventually come up with a form that should be the main form. Even if this form is dynamically created it is still a form class that you set the MainForm property to. Is your question more around dynamically creating a form

     

    -mark

    Program Manager

    Microsoft

    This post is provided "as-is"

     


  • carb

    jbstearley wrote:

    The issue for me is that the main method is imbedded inside a framework.

    This is not true; you can declare the main method wherever you want.

    I am still not understanding why you cant do what you need to do, so lets come at this another way. Tell me exactly why the following wont work for you:



    pulic static void Main(string[] args)
    {
    Form mainForm;
    // Dynamically determine which form to open, create an instance of it, and assign it to mainForm
    Application.Run(mainForm);
    }

    If you can't explain it from this perspective, try posting your ideal code (or pseudo-code), and then explaining why it doesnt work. I am fairly confident your problem can be solved, we just have to clearly define it first.



  • Meher123

    I think where the problem is is that people are reading "framework" and thinking I mean the .NET framework itself. I am referring to a framework that I have built on top of .NET. I want to be able to startup an application within my framework in a consistent manner and so I want the main method "imbedded" within my framework to ensure a consistent application startup mechanism/procedure. If I leave the main method to be implemented by users of the framework, I lose the control of the startup of the application. I don't want to leave the users of the framework with instructions like, "You must implement the Main method in your concrete class that inherits from AbstractStartupClassX and sets your concrete logon form that inherits from LogonFormY to the MainForm property of AbstractStartupClassX." That statement has code smell without even being "code".

    Hopefully this clears up my problem a bit. Sorry for the apparent confusion.

    BTW, I am familiar with the ApplicationContext pattern for swapping startup forms. While I think that is likely 1/2 of solving the problem here, the issue still remains of how do I get the concrete form to set to the ApplicationContext when I have to start the process to get it from within a static Main method that is imbedded within an abstract framework layer on top of the .NET framework. Think 3 and possibly 4 "layers"

    Layer 1 .NET Framework

    Layer 2 "My" framework (want the Main method to reside here)

    Layer 3 Potential Domain layer framework code built on top of "My" framework

    Layer 4 Concrete code for the Application


  • June Low

    You can also use the ApplicationContext object to specify which form, or forms, to display on startup. There is an excellent example in the Visual Studio help, just do a search for ApplicationContext.

    The example demonstrates how to customise the application thread by inherit the ApplicationContext object in order to display two forms at startup.

    You still use the Application.Run() method but instead of a form you specify an instance of your derived context.



  • Larry D.

    I understand that the main method can reside in any class. My point/question is that given that the starting point is inside this static method, the determination of the startup form must also start from within this method, whether it is by instantiating other classes or whatever. The issue for me is that the main method is imbedded inside a framework. The framework has zippo knowledge of the concrete form that is to be used as the startup form or any other concrete class that might be used to generate it for that matter. Given this scenario, the main method has to either "reach up" into the concrete layer to get the form, or the concrete layer (which has to be started by the main method) then has to set the concrete startup form "back" into the ApplicationContext initiated by the main method. Since the main method is static, framework workhorse patterns like Template Method. etc. cannot be used to "get" the form because inheritance cannot be used, or if it can, I have not quite figured out how. Setting the startup form "back" into the framework's ApplicationContext is also problematic as well because the concrete class (the form or some instantiator of it) must be started via some mechanism that also originates within the main method as well. Again the problem being that the main method can have no knowledge of the concrete class/form.

    I hope I've explained the details of the problem well enough this time. I've looked at this from just about every angle I can think of but have yet to come up with a viable solution.


  • Paul Baudouin

    Having the main method embedded in your own framework wont work. You could do this in C++ and MFC because you could use statically linked libraries, where all of the code, including the main method, were compiled into your assembly. But in .NET, there is no such thing as a statically linked library; all code is linked dynamically, and not loaded until run-time. This means that all executable assemblies must define their own Main method, so that the OS knows where the entry point is. You can try to make it as painless as possible for your users, but in the end they will have to define their own Main method which calls into your framework.

  • RealmRPGer

    Mark,

    What I have built is a framework (MVP client along with persistence layer and other server components). I need for the end user of this to be able to extend a framework form, i.e. a "Logon" form and be able within the FW architecture to present that form during the application startup. The static startup prevents usage of inheritance which is problematic in this kind of a scenario. This rules out such framework patterns as Template Method, etc. While I'm new to .NET, I have developed several frameworks in Forte, but the application startup there is just a "startup" class and method within that class to execute. They handle the compile to C++ "under the hood". I really want this mechanism to live in an abstract "client manager" class that the user can extend and override a TemplateMethod to define the concrete startup form but I have not been able to make it work due to the "chicken and egg" problem of the static startup.

    I've seen the derived class from ApplicationContext and then set the MainForm property, but you still have the problem of setting the property to a concrete form; one that is a form derived from some "abstract" form within the FW during the startup from the Main method. While I have been able to figure out things like wiring up multiply nested user controls and form with their associated presenters into a usable tree structure at runtime (despite the less than abstraction friendly designer architecture) but this problem has me stumped thus far.

    Thanks for the help.

    Jim


  • RobertLevy

    The static main method in .NET is just like the entry point of any application in any language. The main method itself cannot be dynamic, because there must always be only one entry poin to an application. However, while it may be a member method of a particular class, that does not mean that that is the only form that can be used as the startup object. All you have to do is determine which form you want to use as your startup object, through whatever dynamic means you find appropriate, instantiate that form, and then pass that form to Application.Run. All this can be done from within the Main method, no matter where it lives (it doesnt even have to be a member method of a form; that is only default behavior. the main method can be a member of any class in your assembly).
  • Determining Application Startup Form at run time