IMZRH的日志

努力成为一个有用的人

导航

如何确保应用只有一个实例运行

Posted on 2006-11-17 21:56  张荣华  阅读(790)  评论(1编辑  收藏  举报
 平时,我们经常可以遇到有的应用正在运行,这时我们再点击应用程序的快捷方式,就会出现一个提示我们应用程序正在运行的提示。这是有一很实用的技巧。那么它是怎么实现的呢?其实很简单,只要在程序中加入以下的代码可以实现了。
 首先把project的Main方法改成
public static void Main()
{
   
try
   
{
               RegistryKey key 
= Registry.LocalMachine.OpenSubKey(@"software\zrh");// 读注册表以判断是否正在运行 
                if (key == null)//如果不存在,就创建标志位
                
                             RegistryKey writekey 
= Registry.LocalMachine.CreateSubKey(@"software\zrh");
                             writekey.SetValue(
"instanceFlag""1");            
                       
/*调用应用程序代码*/
                    }

                
else
              
{
                        MessageBox.Show(
"the application has running");                
                   }

           }

            
catch (Exception ex)
            
{
                MessageBox.Show(
" on exception is occuring \n" + ex.Message);
                }

    }
其次在应用退出时加入以下代码,以便删除注册表中的内容,否则以后就不能打开该project了。
    try
            {
                Microsoft.Win32.RegistryKey flagKey 
= Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"software"true);     
                flagKey.DeleteSubKey(
"zrh");
            }

            
catch (Exception ex)
           
{
                MessageBox.Show(
" on exception is occuring \n" + ex.Message);
            }

经过以上的设置,就可以达到让应用程序只有一个实例的效果了,是不是很简单?当然,达到一个目的的方法有很多,这只是其中的一种。


     看了feiyun0112的回复,觉得很有道理,就又整理了两种方法,特附录如下:

方法一:使用Mutex来进行

1.  首先要添加如下的namespace

using System.Threading;

 

2.  修改系统Main函数,大致如下:

        bool bCreatedNew;

       

        //Create a new mutex using specific mutex name

        Mutex m =new Mutex( false, "myUniqueName", out bCreatedNew );

        if( bCreatedNew )

            Application.Run(new yourFormName());

 

如上面编码就可以了,要注意的一点是,在给Mutex起名字的时候,不要太简单,以防止和其他程序的Mutex重复,从而达不到所预想的效果。

 

方法二:使用Process来进行

1.  首先要添加如下的namespace

using System.Diagnostics;

using System.Reflection;

 

2.  添加如下函数:

        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;

        }

 

3.  修改系统Main函数,大致如下:

            if( RunningInstance() == null )

                Application.Run(new yourFormName());

 

如上面编码就可以了,要注意的一点是,在判断进程模块文件名是否相等这部分的代码,是可选的。如果当前的程序在文件系统中只存在一个的话,以上的方法是可以的;否则不要删除这部分的代码。