51

The Main method is the entry point of a C# console application. Thus, for example, if I have to start some threads or services, I will do it within the Main method.

I do not see the Main method inside a WPF project, so what is the entry point of a WPF application? If I have to start some threads or services, where should write the code for starting them?

UPDATE: this answer summarizes the available solutions, but what are the pros and cons of each solution?

Community
  • 1
  • 1
enzom83
  • 7,500
  • 8
  • 55
  • 107
  • related replacing it discussed here http://stackoverflow.com/questions/6156550/replacing-the-wpf-entry-point – kenny Aug 18 '14 at 20:48
  • Re the update: the answer mentions the pros : earlier and earlier. It's a matter of when with respect to other code. And there is something as "too early". – Henk Holterman Aug 18 '14 at 21:22

4 Answers4

56

For a WPF standalone application that is generated in Visual Studio using the New Project wizard, the entry point for the application is the Main function, defined in App.g.cs (generated code). In the default project, this is the public static void App.Main method.

Check this

In general, a .NET application will use as its entry point (first function called) any method named Main that has public/static access modifiers–no matter what class Main is located in.

If your application has more than one class with a public static Main method, you’ll need to specify the entry point in the project properties dialog. In the Startup object dropdown, select the class that contains the Main method that should be called on startup.

  • 1
    But can the OP update the code in App.g.cs and use for his purpose ? From the normal App.xaml.cs, I verified "Main" method cannot be overridden/ reused. So, what's the point ? – Ron16 Mar 10 '17 at 08:55
  • It says App.g.i.cs, not App.g.cs. – Kyle Delaney Mar 17 '17 at 19:04
  • 1
    @Ron16: The point is that there is still a `Main` -- and its contents show how to kickstart a WPF app. Although you can't customize the generated entry point, you can build your own that does the same thing (possibly after configuring the app). – cHao Mar 13 '18 at 15:31
  • Upvoted for explaining that the startup of the project can be set in the project properties. This is useful when the `app.xaml.cs` code-behind file has multiple potential entry points. – J Weezy Apr 05 '21 at 17:04
44

Your main entry point is an override of OnStartup in the code-behind of App.Xaml :

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        // here you take control
    }
}

Other points of interest might be Application.OnActivate() and the Loaded and Initialized events of your MainWindow.

If I have to start some threads or services, where should write the code for starting them?

Depends on what those threads/services need and want.

Henk Holterman
  • 236,989
  • 28
  • 287
  • 464
  • Well, the threads could be started immediately when the application starts, while the services should be started when the user clicks a button on the UI. In this case, I think that the thread can be started in the `OnStartup` method, while the services should be started after the UI is ready. Is this correct? – enzom83 Aug 18 '14 at 21:33
  • 1
    I don't / can't know. Depends on what they do. A thread that needs the UI should start later. – Henk Holterman Aug 18 '14 at 21:49
  • Why should we call `base.OnStartup(e)`? – Altiano Gerung Jun 26 '18 at 03:02
  • Why does this Main() not appear when I do a Ctrl-F search in Visual Studio? I have "Entire Solution" selected. – InvalidBrainException Apr 28 '19 at 22:14
  • @AltianoGerung it's simply good form. When we override a method on a superclass, often we simply want a way to tap into a process without disrupting its flow. We may not know exactly what the method is/might one day be doing to the state so it's safest to preserve it. – ne1410s Dec 08 '20 at 19:26
12

The Main for a WPF application is autogenerated and can be found in one of the .cs files that backs your App.xaml file. You can expand App.xaml -> App.xaml.cs -> App -> Main() in the solution explorer, which will get you to the App.g.i.cs source file, which contains your Main() function.

This file is auto-generated, so rather than editing the Main there, I would recommend creating a new .cs file in your project that contains the Main() function. You then have to change the properties of your project to specify the correct startup object. This is done on the Application tab in your project properties. Set it to the class that contains your custom Main function.

You probably want to copy the contains of the autogenerated Main into your new one, since you want your application to behave normally (show the main window, etc.).

lordjeb
  • 1,225
  • 9
  • 14
  • Where can I find App.g.i.cs in the solution explorer? – Kyle Delaney Mar 17 '17 at 19:05
  • @KyleDelaney usually in obj/Debug. For any question like this you can open the file in visual studio (i.e. using the method mentioned in this answer) and hover over the tab with the file's name in the text editor. – Assimilater Jun 22 '17 at 21:39
2

Entry point is App.xaml.cs typically.

You want to avoid putting code there ideally. Instead try instantiating them in view models for MVVM. It's typically a tricky place to find stuff - as your question is testament to.

Another alternative, load them in a helper class and then instantiate that in the app.xaml file.

kidshaw
  • 3,189
  • 2
  • 13
  • 25