1.3 Creating Multiple Application Entry Points

 <  Day Day Up  >  

You want your application to contain several entry points.


Technique

For each entry point you want to add, create a separate class containing a static Main member function. To change which entry point is run by the CLR, go to the project properties (Alt+P,P) and select Common Properties, General. Select the function you want to use as the application entry point in the Startup Object field. You must then recompile your project any time you change the application entry point because you can only change the entry point at compile time, not at runtime.

Comments

There are several reasons why creating several application entry points is advantageous. One key advantage is that it allows you to control the flow of your application to test different avenues of functionality. For instance, you can create an entry point that serves to display a tremendous amount of debugging information at runtime. When you are confident that the unit tests you created are working correctly, you can create a final application entry point that removes most of the debugging information. However, in the past, you might have had to comment out or remove the critical debugging information. By creating a separate entry point, you gain the advantage of preserving your unit tests that you can run simply by changing a property within your project. With a little clever command-line compiling, you can even create an automated test harness that compiles your application, specifying one of your unit-test application entry points; verify that the results are satisfied; and repeat that process for each entry point.

To compile a C# program from the command line and specify which startup object to use as the application entry point, use the /main command-line argument for the compiler. Listing 1.2 contains a project source file that contains three different application entry points. To compile this file on the command line and specify an application entry point, you invoke the C# compiler using the following:

 
 csc.exe Class1.cs /main:_3_MultEntryPoints.EntryPointTest.Main3Entry 
Listing 1.2 A C# Application Containing Multiple Application Entry Points
 using System; namespace _3_MultEntryPoints {    class EntryPointTest     {         class Main1Entry         {             [STAThread]             static void Main(string[] args)             {                 Console.WriteLine( "Running in Main" );             }         }         class Main2Entry         {             [STAThread]             static void Main(string[] args)             {                 Console.WriteLine( "Running in Main2" );             }         }         class Main3Entry         {             [STAThread]             static void Main(string[] args)             {                 Console.WriteLine( "Running in Main3" );             }         }     } } 
 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net