问题
In my project I am having more than two Main method with same signature. One is a winForm and other one is Console class.
How to set any one of them as entry point.
I declared [STAThread] before one main method to set entry point but It's not working?
I am using Visual Studio express 2010
回答1:
Go into the project properties by right clicking the project in the solution explorer and click properties. On the first tab you will see a drop down list for the entry point. Select the appropriate main method.
回答2:
STAThread doesn't set the entry point - it forces the runtime to use a Single-Threaded Apartment for the thread which executes Main.
As Josh says, you need to set the Startup Object in project properties. If that isn't showing in VS Express, you may need to force it to show advanced build properties: Open Tools\Options and check "Show advanced build configuration" in Projects and Solutions.
Note that your application either has to be a console app or a WinForms app. It can only be built as one (per configuration, anyway). You'll either end up with a console showing when you start is as a WinForms app, or no console showing when you start it as a console app.
Have you considered putting the bulk of the logic in a class library, and then creating two wrapper applications - one WinForms and one console - which just display the relevant UI and then delegate to the class library?
回答3:
If program have two main method compiler get confution so run c# program put main class give
public Class one
{
public static void main()
{
System.console.writeline("One");
}
public Class two
{
public static void main()
{
System.console.writeline("Two");
}
}
run C# in console
CSC Multimain.cs /main:one
- Output One
CSC Multimain.cs /main:two
- Output Two
回答4:
You can place a Main method in every class you declare. Some programmers take advantage of this to build a small test app into each class they declare.
However, if you declare more than one Main method among the classes of your project, you’ll need to indicate to the IDE which one you would like to be the app’s entry point. To do so:
- With the project open in Visual Studio, select Project > [ProjectName] Properties... (where [ProjectName] is the name of your project).
- Select the class containing the Main method that should be the entry point from the Startup object list box.
回答5:
You can put more then one main method in single program but program with one main method will be compiled at a time for example : Copy paste this code in editor and change name Release in first line and see the change
#define Release
using System;
class Program
{
#if Release==true
public static void Main(string[] args) //for enemy
{
Console.WriteLine("go to hell");
Console.ReadLine();
}
#elif Release==false
static void Main(string[] args) //for friend
{
Console.WriteLine("hello ");
Console.ReadLine();
}
#endif
}
回答6:
you can also do by this way but i dont know how to set which main method will be entry point
public static void Main(string args)
{
Console.WriteLine("this is second main method");
}
public static void Main(string[] args)
{
Program.Main("second main method");
}
回答7:
This issue could be solved easily by steps here 1. Open the solution 2 split the two classes into separate class files 3. click on the "Project" tab from main menu bar 4. Move to cursor to the Property 5. Select the desired class name you wanted to run for "Reference Path" 6. Compile the project, the error will be ride off.
回答8:
I do not think this will work. It won't even compile and complaint that the project has two main methods. There must only be one public static main method in the project. You have to rename/remove the second one.
By the way exactly why would you like to keep both of them?
来源:https://stackoverflow.com/questions/4406026/more-than-two-main-method-in-visual-studio-application