How to make my app singleton application? [duplicate]

拟墨画扇 提交于 2020-01-04 08:23:12

问题


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 .


回答1:


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.




回答2:


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());
    }
}



回答3:


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



来源:https://stackoverflow.com/questions/6437056/how-to-make-my-app-singleton-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!