34

I deployed my C# WinForms application using ClickOnce installation. Everything works fine with it (after a lot of work) :), but now I'm facing a problem:

Whenever I click on the application shortcut in the Start menu, a new instance starts. I need to avoid this.

What can I do to prevent multiple launches?

Tolga Evcimen
  • 6,438
  • 10
  • 52
  • 82
  • 1
    possible duplicate of [What is a good pattern for using a Global Mutex in C#?](http://stackoverflow.com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c) – Oliver Hanappi Feb 27 '13 at 15:00
  • 1
    possible duplicate of [Prevent multiple instances of a given app in .NET?](http://stackoverflow.com/questions/93989/prevent-multiple-instances-of-a-given-app-in-net) – joce Apr 13 '13 at 00:42
  • See [this](https://stackoverflow.com/questions/6392031/how-to-check-if-another-instance-of-the-application-is-running) - Patrik Svensson answer. – kammer May 02 '19 at 09:39
  • @kammer it's been more than 6 years :D – Tolga Evcimen May 02 '19 at 13:19

10 Answers10

43

At program startup check if same process is already running:

using System.Diagnostics;

static void Main(string[] args)
{
   String thisprocessname = Process.GetCurrentProcess().ProcessName;

   if (Process.GetProcesses().Count(p => p.ProcessName == thisprocessname) > 1)
      return;           
}
MikroDel
  • 6,389
  • 7
  • 37
  • 69
semao
  • 1,627
  • 12
  • 12
  • 6
    I think that should see this: http://odetocode.com/blogs/scott/archive/2004/08/20/the-misunderstood-mutex.aspx – SubmarineX Apr 30 '14 at 08:06
36

Use this code:

[STAThread]
static void Main() 
{
   using(Mutex mutex = new Mutex(false, "Global\\" + appGuid))
   {
      if(!mutex.WaitOne(0, false))
      {
         MessageBox.Show("Instance already running");
         return;
      }

      Application.Run(new Form1());
   }
}

from The Misunderstood Mutex

MikroDel
  • 6,389
  • 7
  • 37
  • 69
  • I was using a different Mutex code than above which worked locally, but once I started using ClickOnce it didn't work. The above code works for me in ClickOnce – Harag Feb 24 '15 at 11:04
  • @harag - glad to help you :) Vote the answer up please if it was usefull for you – MikroDel Feb 25 '15 at 08:46
  • 6
    where do you get the appGuid from? – Rafael Herscovici Jun 08 '15 at 09:31
  • What do you do if you are using C#/WPF where there is no explicit main method? I don't want to explicitly declare a main since it is being auto-generated with some additional content. – yellavon Jul 10 '15 at 15:09
3

There is really good topic on that matter. You can find it here: using Mutext.

rusev
  • 1,755
  • 2
  • 16
  • 14
3

In WPF, you can use this code in your App.xaml.cs file:

private static System.Threading.Mutex _mutex = null;

protected override void OnStartup(StartupEventArgs e)
{
    string mutexId = ((System.Runtime.InteropServices.GuidAttribute)System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false).GetValue(0)).Value.ToString();
    _mutex = new System.Threading.Mutex(true, mutexId, out bool createdNew);
    if (!createdNew) Current.Shutdown();
    else Exit += CloseMutexHandler;
    base.OnStartup(e);
}
protected virtual void CloseMutexHandler(object sender, EventArgs e)
{
    _mutex?.Close();
}

DKH
  • 372
  • 3
  • 12
1

When starting you application, main always calls Application.Run(). Look in your STAThread-Main method and before Application.Run test, if there are running instances of your .exe.

Process p = Process.GetProcesses();
//check for your .exe

See this post here.

Community
  • 1
  • 1
bash.d
  • 12,357
  • 2
  • 24
  • 37
1

There are times **Mutex** is not working in some areas. Like using in console application. So I tried using WMI Query.

Try this and it will work.

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (!isStillRunning())
            {
               Application.Run(new Form1());
             }
             else {
                 MessageBox.Show("Previous process still running.",
                    "Application Halted", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                 Application.Exit();
             }      
        }

        //***Uses WMI Query
        static bool isStillRunning() {
            string processName = Process.GetCurrentProcess().MainModule.ModuleName;
            ManagementObjectSearcher mos = new ManagementObjectSearcher();
            mos.Query.QueryString = @"SELECT * FROM Win32_Process WHERE Name = '" + processName + @"'";
            if (mos.Get().Count > 1)
            {
               return true;
            }
            else
               return false;
        }

Hope it helps.

Mardoxx
  • 3,964
  • 5
  • 33
  • 57
Israel Ocbina
  • 411
  • 6
  • 10
0

what i always using is

bool checkSingleInstance()
    {
        string procName = Process.GetCurrentProcess().ProcessName;
        // get the list of all processes by that name

        Process[] processes = Process.GetProcessesByName(procName);

        if (processes.Length > 1)
        {

            return true;
        }
        else
        {
            return false;
        }
    }
0

solution in Windows form application Prohibit again run application(reopen application).

1- first add Class RunAlready.cs

2-Call method processIsRunning() with Name Process from RunAlready.cs in Program.cs

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tirage.MainStand
{
static class Program
{

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        PublicClass.Class.RunAlready RunAPP = new PublicClass.Class.RunAlready();
        string outApp = RunAPP.processIsRunning("Tirage.MainStand");

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        MainStand_FrmLogin fLogin = new MainStand_FrmLogin();
        if (outApp.Length == 0)
        {

            if (fLogin.ShowDialog() == DialogResult.OK)
            {
                Application.Run(new MainStand_masterFrm());

            }
        }
        else MessageBox.Show( "Instance already running");

      }
    }
 }

class RunAlready:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PublicClass.Class
{
  public  class RunAlready
    {
      public  string processIsRunning(string process)
        {
        string xdescription = "";
        System.Diagnostics.Process[] processes =
            System.Diagnostics.Process.GetProcessesByName(process);
        foreach (System.Diagnostics.Process proc in processes)
        {
            var iddd = System.Diagnostics.Process.GetCurrentProcess().Id;
            if (proc.Id != System.Diagnostics.Process.GetCurrentProcess().Id)
            {
                xdescription = "Application Run At time:" + proc.StartTime.ToString() + System.Environment.NewLine;
                xdescription += "Current physical memory : " + proc.WorkingSet64.ToString() + System.Environment.NewLine;
                xdescription += "Total processor time : " + proc.TotalProcessorTime.ToString() + System.Environment.NewLine;
                xdescription += "Virtual memory size : " +         proc.VirtualMemorySize64.ToString() + System.Environment.NewLine;
            }
        }


        return xdescription;
    }
}
}
0
 if (Process.GetProcesses().Count(p => p.ProcessName == "exe name") > 1)
    {
        foreach (var process in 
 Process.GetProcessesByName("exe name"))
        {
            process.Kill();
        }
    }
LovExpert
  • 17
  • 7
-3

Using Mutex is the way to go because guessing process names is full of flaws and niggles. Check out this really nice illustration

user3195836
  • 1
  • 1
  • 1
  • why do you give the same answer as already was given one year before? http://stackoverflow.com/a/15115327/502950 – MikroDel Jan 16 '14 at 06:32