如何让我的应用程序单应用程序? [英] How to make my app singleton application?

查看:122
本文介绍了如何让我的应用程序单应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,但目前它不是一个单独的应用程序。 我喜欢这样,它的另一个实例不会退出在运行时,使其单应用程序。

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 .

如果可以做到这一点,请用一些样本codeS答复。

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

推荐答案

下面是一些很好的示例应用程序。下面是一种可能的方式。

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.

第一个。

第二个。

第三个

这篇关于如何让我的应用程序单应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆