Limiting a WinForm Application to a Single Process with Multiple Instances

We have a WinForms application in C # 3.5 (SP 1). We would like to limit the application to a single process in memory with multiple instances of windows. We are not looking for an MDI approach, but we have separate instances of the main form without running multiple application processes.

We evaluate three approaches:

  • Windows Messages
  • COM
  • WCF

We have rough details for the first 2 (PInvoke with Windows messages and WinProc overrides, COM Registry, etc.) and they use older technologies.

We discussed the idea of ​​using WCF with named pipes and think that this might be the easiest and easiest way to complete the task.

What is the cleanest and most modern way to limit an application to a single process with multiple instances of the main form?

+5
2

, , - mutex. , mutex , , , bamo! , .

public static void CheckSingleInstance()
{
    bool created = false;
    _singleInstance = new Mutex(true, "PredefinedMutexName##1", out created);

    if (!created)
    {
        MessageBox.Show("Another instance of this application is already running. Click OK to switch to that instance.", "Application running", MessageBoxButtons.OK, MessageBoxIcon.Information);

        Process proc = Process.GetCurrentProcess();
        Process[] procs = Process.GetProcessesByName(proc.ProcessName);
        bool exit = false;
        Process process = null;
        foreach (Process p in procs)
        {
            if (p.Id != proc.Id && p.MainModule.FileName == proc.MainModule.FileName)
            {
                // ShowWindow(p.MainWindowHandle, 1/*SW_SHOWNORMAL*/);
                process = p;
                exit = true;
            }
        }
        if (exit)
        {
            Application.Exit();

            if (process != null)
                NativeMethods.SetForegroundWindow(process.MainWindowHandle);

            return;
        }
    }
}
+3

. , , , , , ? ?

. , , . , , ( ..).

/ WCF, .NET Framework

+1

All Articles