I'm trying to make a single instance WPF application using the hints on:
What is the correct way to create a single-instance application?
This ultimately requires changes to Main(). In WPF, Main() seems to be auto-generated. I'd rather not modify autogenerated code. Is there a way to suppress Main from being auto-generated?
(alternatively, if you know of a better single app instance pattern for WPF that doesn't rely on modifying auto-generated code, please suggest it)
From the blog: http://bengribaudo.com/blog/2010/08/26/136/wpf-where-is-your-static-main-method
The following two methods will avoid the duplicate Main collision:
Tell the compiler that your static Main() method should be the execution entry point—Set your project’s “Startup object” setting to the class containing your static Main() method (right-click on the project in Solution Explorer, choose “Properties,” then look for the “Startup object” setting under the “Application” tab). (This was also mentioned by Bahri Gungor)
Turn off auto-generation of App.g.cs’s static Main() method—In Solution Explorer, right click on App.xaml, choose “Properties,” then change the “Build Action” from “ApplicationDefinition” to “Page”.
Actually, in a default WPF project, the application startup object is the App class (code-behind for app.xaml).
You can write your own class, create the startup code however you like, and start your application like this:
public class Startup
{
[STAThread]
public static void Main(string[] args)
{
// Check for existing instance (mutex or w/e) here
//
App app = new App();
app.Run();
}
}
You can change the startup object in your project file.
I wouldn't use the mutex approach for creating a single instance WPF app. I would use the answer described here - https://stackoverflow.com/a/19326/248164.
This is also the approach described in Pro WPF in C# 2010.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With