0

I have an application that should only be allowed to run one instance per user session. If the user clicks to launch the application again, I want to bring the one already to focus. I followed the steps in this tutorial WPF Single Instance Application

and the steps in this tutorial is :

Step 1: Add the file SingleInstance.cs to your project.

Step 2: Add a reference to your project: System.Runtime.Remoting.

Step 3: Have your application class implement ISingleInstanceApp (defined in SingleInstance.cs).

The only method in this interface is:

Hide Copy Code bool SignalExternalCommandLineArgs(IList args) This method is called when a second instance of your application tries to run. It has an args parameter which is the same as the command line arguments passed to the second instance.

Step 4: Define your own Main function that uses the single instance class.

Your App class should now be similar to this:

Hide Copy Code /// Step 5: Set new main entry point.

Select Project Properties –> Application and set “Startup object” to your App class name instead of “(Not Set)”.

Step 6: Cancel the default WPF main function.

Right click on App.xaml, Properties, set Build Action to "Page" instead of "Application Definition".

I stick at step 4 I don't know how to define my own Main function that uses the single instance class? anyone help me please, thanks

  • Have a look at [My Question](http://stackoverflow.com/questions/38473277/wpf-single-instance-window-clickonce-pass-arguments) and ignore the Parameter-Part – lokusking Aug 08 '16 at 21:41

2 Answers2

0

Okay it is so easy add this method to your App.xaml.cs file in the App Class:

[STAThread]    
public static void Main(string[] args)
{
    if (SingleInstance<App>.InitializeAsFirstInstance("MyApp"))
    {
        var app = new App();
        app.InitializeComponent();
        app.Run();
        // Allow single instance code to perform cleanup operations
        SingleInstance<App>.Cleanup();
    }
}
Henka Programmer
  • 727
  • 7
  • 25
  • GRADApp didn't accept and there is a red line under it why? – بسمة أمل Aug 08 '16 at 19:05
  • it became in the blue color but still had a red line under it and the error is saying "the type of MyApp.App cannot be use as type parameter TApplication is the generic type or method Microsoft.Shell.SingleInstance there is no implicit reference conversion from MyApp.App to Microsoft.Shell.SingleInstanceApp – بسمة أمل Aug 09 '16 at 06:21
-1
using System.Threading;

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
   bool createdNew = true;
   using (Mutex mutex = new Mutex(true, "MyApplicationName", out createdNew))
   {
      if (createdNew)
      {
         Application.EnableVisualStyles();
         Application.SetCompatibleTextRenderingDefault(false);
         Application.Run(new MainForm());
      }
      else
      {
         Process current = Process.GetCurrentProcess();
         foreach (Process process in Process.GetProcessesByName(current.ProcessName))
         {
            if (process.Id != current.Id)
            {
               SetForegroundWindow(process.MainWindowHandle);
               break;
            }
         }
      }
   }
}
Arun Selva Kumar
  • 2,593
  • 3
  • 19
  • 30
  • Application.Run(new MainWindow()); didn't accept in my code and the error is ===> the best overloaded method for System.Windows.Forms.Application.Run (System.Windows.Forms.ApplicationContext has same invalid arguments – بسمة أمل Aug 08 '16 at 19:47
  • This is an WinForms Solution. Not really helpful on WPF – lokusking Aug 08 '16 at 21:39