I have the following requirements to fulfill in a WPF application:
- When you open the program (by clicking its shortcut or double-clicking its executable), it opens its MainWindow normally;
- If you try to open it while another instance is running, instead of opening another window, it brings the already-running to the front, and closes.
Currently I have already a way of detecting an open instance, by using Mutex
, but I don't know what I could do to call that instance's MainWindow to the front.
public partial class App : Application
{
public static Mutex mutex;
[STAThread]
public static void Main(string[] args)
{
if (!IsFirstInstance())
{
CallOtherToFront(); // how to implement this??
return;
}
var app = new App();
app.InitializeComponent();
app.Run();
}
public static bool IsFirstInstance()
{
mutex = new Mutex(true, "SmartFictitiousApplication");
return mutex.WaitOne(TimeSpan.Zero, false);
}
}