1

I have a application but currently it is not a singleton application. I like to make it singleton application so that its another instance does not exit at the run time .

If this can be done please reply with some sample codes .

CharithJ
  • 46,289
  • 20
  • 116
  • 131
cmthakur
  • 2,266
  • 4
  • 18
  • 23
  • 3
    You should specify if you want one instance per desktop, per logged in user, per computer,... It's common that programs choose "per computer" when one of the other choices is more appropriate. – CodesInChaos Jun 22 '11 at 08:34
  • See http://stackoverflow.com/questions/2988960/how-to-make-application-singleton – ChrisF Jun 22 '11 at 08:36
  • " its another instance does not exit at the run time " what do you mean by that? The only way I know is that the newly started program checks in some way if there is already an instance and then exits(usually after activating the old instance and perhaps sending it some messages). – CodesInChaos Jun 22 '11 at 08:38

3 Answers3

4

I think the following codes will be helpful for you. Here is the related link: http://geekswithblogs.net/chrisfalter/archive/2008/06/06/how-to-create-a-windows-form-singleton.aspx

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        /*====================================================
         * 
         * Add codes here to set the Winform as Singleton
         * 
         * ==================================================*/
        bool mutexIsAvailable = false;

        Mutex mutex = null;

        try
        {
            mutex = new Mutex(true, "SampleOfSingletonWinForm.Singleton");
            mutexIsAvailable = mutex.WaitOne(1, false); // Wait only 1 ms
        }
        catch (AbandonedMutexException)
        {
            // don't worry about the abandonment; 
            // the mutex only guards app instantiation
            mutexIsAvailable = true;
        }

        if (mutexIsAvailable)
        {
            try
            {
                Application.Run(new SampleOfSingletonWinForm());
            }
            finally
            {
                mutex.ReleaseMutex();
            }
        }

        //Application.Run(new SampleOfSingletonWinForm());
    }
}
Mister.Z
  • 41
  • 2
3

Here are some good sample applications. Below is one possible way.

public static Process RunningInstance() 
{ 
    Process current = Process.GetCurrentProcess(); 
    Process[] processes = Process.GetProcessesByName (current.ProcessName); 

    //Loop through the running processes in with the same name 
    foreach (Process process in processes) 
    { 
        //Ignore the current process 
        if (process.Id != current.Id) 
        { 
            //Make sure that the process is running from the exe file. 
            if (Assembly.GetExecutingAssembly().Location.
                 Replace("/", "\\") == current.MainModule.FileName) 

            {  
                //Return the other process instance.  
                return process; 

            }  
        }  
    } 
    //No other instance was found, return null.  
    return null;  
}


if (MainForm.RunningInstance() != null)
{
    MessageBox.Show("Duplicate Instance");
    //TODO:
    //Your application logic for duplicate 
    //instances would go here.
}

Many other possible ways. See the examples for alternatives.

First one.

Second One.

Third One.

Community
  • 1
  • 1
CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • 1
    `Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName` don't use `process`. Are you sure it's not a bug? Is so it may be moved outside `foreach` – Poma Aug 23 '13 at 18:56
1

The approach I know of is the following. The program must attempt to open a named mutex. If that mutex existed, then exit, otherwise, create the mutex. But this seems to contradict your condition that "its another instance does not exit at the run time". Anyway, maybe this too was helpful

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434