I am trying to build a single instance WPF app using c#. I follow this answer to create the following app. However, the OnStartupNextInstance function never gets called no matter how many times I re-launch the same app (double-clicking the *.exe file).
There is no exceptions or anything printed in the Debug Output.
Has anyone got a single instance WPF app working using this approach? What am I missing here?
using Microsoft.VisualBasic.ApplicationServices;
using System;
using System.Windows;
namespace WpfApp1
{
public class EntryPoint
{
[STAThread]
public static void Main(string[] args)
{
var man = new SingleInstanceManager();
man.Run(args);
}
}
public class SingleInstanceManager : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
{
public SingleInstanceManager()
{
IsSingleInstance = true;
}
protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
{
MessageBox.Show("First time launch");
var app = new App();
app.InitializeComponent();
app.Run();
return false;
}
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
{
MessageBox.Show("Subsequent launch");
base.OnStartupNextInstance(eventArgs);
eventArgs.BringToForeground = true;
}
}
}