0

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);
    }
}
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
  • @vipersassassin Actually the most important part of my question is to bring the other instance's MainWindow to the front. This could be made either from the second instance (my original thought) or by sending a message to the first one (as I have seen being suggested somewhere else). – heltonbiker Apr 13 '17 at 16:33
  • That is answered in the other question as well. – vipersassassin Apr 13 '17 at 16:49
  • @vipersassassin yeah, you're right, it was a bit overwhelming. Gonna take a look and come back to accept the duplicate suggestion if appropriate. Thanks! – heltonbiker Apr 13 '17 at 17:04

0 Answers0