【第22期】观点:IT 行业加班,到底有没有价值?

C#基于.Net平台应用程序唯一运行实例C#代码实现

转载 2011年01月13日 16:39:00

主要代码
SingleInstance.cs文件,

using System;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Zhengzuo.CSharpCode
{
    /// <summary>
    /// 只启动一个应用程序实例控制类
    /// </summary>
    public static class SingleInstance
    {
        private const int WS_SHOWNORMAL = 1;
        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        //标志文件名称
        private static string runFlagFullname = null;
        //声明同步基元
        private static Mutex mutex = null;

        /// <summary>
        /// static Constructor
        /// </summary>
        static SingleInstance()
        {
        }
        #region api实现      
        /// <summary>
        /// 获取应用程序进程实例,如果没有匹配进程,返回Null
        /// </summary>
        /// <returns>返回当前Process实例</returns>
        public static Process GetRunningInstance()
        {            
            Process currentProcess = Process.GetCurrentProcess();//获取当前进程
            //获取当前运行程序完全限定名
            string currentFileName = currentProcess.MainModule.FileName;
            //获取进程名为ProcessName的Process数组。
            Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
            //遍历有相同进程名称正在运行的进程
            foreach (Process process in processes)
            {
                if (process.MainModule.FileName == currentFileName)
                {
                    if (process.Id != currentProcess.Id)//根据进程ID排除当前进程
                        return process;//返回已运行的进程实例
                }
            }
            return null;
        }
        /// <summary>
        /// 获取应用程序句柄,设置应用程序前台运行,并返回bool值
        /// </summary>
        public static bool HandleRunningInstance(Process instance)
        {
            //确保窗口没有被最小化或最大化
            ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
            //设置真实例程为foreground window
            return SetForegroundWindow(instance.MainWindowHandle);
        }
        /// <summary>
        /// 获取窗口句柄,设置应用程序前台运行,并返回bool值,重载方法
        /// </summary>
        /// <returns></returns>
        public static bool HandleRunningInstance()
        {
            Process p = GetRunningInstance();
            if (p != null)
            {
                HandleRunningInstance(p);
                return true;
            }
            return false;
        }
        #endregion

        #region Mutex实现
        /// <summary>
        /// 创建应用程序进程Mutex
        /// </summary>
        /// <returns>返回创建结果,true表示创建成功,false创建失败。</returns>
        public static bool CreateMutex()
        {
            return CreateMutex(Assembly.GetEntryAssembly().FullName);
        }
        /// <summary>
        /// 创建应用程序进程Mutex
        /// </summary>
        /// <param name="name">Mutex名称</param>
        /// <returns>返回创建结果,true表示创建成功,false创建失败。</returns>
        public static bool CreateMutex(string name)
        {
            bool result = false;
            mutex = new Mutex(true, name, out result);
            return result;  
        }
        /// <summary>
        /// 释放Mutex
        /// </summary>
        public static void ReleaseMutex()
        {
            if (mutex != null)
            {
                mutex.Close();
            }
        }
        #endregion

        #region 设置标志实现
        /// <summary>
        /// 初始化程序运行标志,如果设置成功,返回true,已经设置返回false,设置失败将抛出异常
        /// </summary>
        /// <returns>返回设置结果</returns>
        public static bool InitRunFlag()
        {
            if (File.Exists(RunFlag))
            {
                return false;
            }
            using (FileStream fs = new FileStream(RunFlag, FileMode.Create))
            {
            }
            return true;
        }
        /// <summary>
        /// 释放初始化程序运行标志,如果释放失败将抛出异常
        /// </summary>
        public static void DisposeRunFlag()
        {
            if (File.Exists(RunFlag))
            {
                File.Delete(RunFlag);
            }
        }  

        /// <summary>
        /// 获取或设置程序运行标志,必须符合Windows文件命名规范
        /// 这里实现生成临时文件为依据,如果修改成设置注册表,那就不需要符合文件命名规范。
        /// </summary>
        public static string RunFlag
        {
            get
            {
                if(runFlagFullname == null)
                {
                    string assemblyFullName = Assembly.GetEntryAssembly().FullName;
                    //CommonApplicationData://"C://Documents and Settings//All Users//Application Data"
                    string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                    //"C://Program Files//Common Files"
                    //string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles);
                    runFlagFullname = Path.Combine(path, assemblyFullName);
                }
                return runFlagFullname;
            }
            set
            {
                runFlagFullname = value;
            }
        }
        #endregion
    }
}

Program.cs文件,

using System;
using System.Windows.Forms;
using System.Diagnostics;
using Zhengzuo.CSharpCode;
namespace Zhengzuo.Test.WinGui
{
    static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length == 0) //没有传送参数
            {
                Process p = SingleInstance.GetRunningInstance();
                if (p != null) //已经有应用程序副本执行
                {
                    SingleInstance.HandleRunningInstance(p);
                }
                else //启动第一个应用程序
                {
                    RunApplication();
                }
            }
            else //有多个参数
            {
                switch (args[0].ToLower())
                {
                    case "-api":
                        if (SingleInstance.HandleRunningInstance() == false)
                        {
                            RunApplication();
                        }
                        break;
                    case "-mutex":
                        if (args.Length >= 2) //参数中传入互斥体名称
                        {
                            if ( SingleInstance.CreateMutex(args[1]) )
                            {
                                RunApplication();
                                SingleInstance.ReleaseMutex();
                            }
                            else
                            {
                                //调用SingleInstance.HandleRunningInstance()方法显示到前台。
                                MessageBox.Show("程序已经运行!");
                            }
                        }
                        else
                        {
                            if (SingleInstance.CreateMutex())
                            {
                                RunApplication();
                                SingleInstance.ReleaseMutex();
                            }
                            else
                            {
                                //调用SingleInstance.HandleRunningInstance()方法显示到前台。
                                MessageBox.Show("程序已经运行!");
                            }
                        }
                        break;
                    case "-flag"://使用该方式需要在程序退出时调用
                        if (args.Length >= 2) //参数中传入运行标志文件名称
                        {
                            SingleInstance.RunFlag = args[1];                          
                        }
                        try
                        {
                            if (SingleInstance.InitRunFlag())
                            {
                                RunApplication();
                                SingleInstance.DisposeRunFlag();
                            }
                            else
                            {
                                //调用SingleInstance.HandleRunningInstance()方法显示到前台。
                                MessageBox.Show("程序已经运行!");
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }
                        break;
                    default:
                        MessageBox.Show("应用程序参数设置失败。");
                        break;
                }
            }
        }
        //启动应用程序
        static void RunApplication()
        {
            Application.EnableVisualStyles();
            Application.Run(new MainForm());
        }
    }
}

举报

相关文章推荐

基于.Net平台应用程序唯一运行实例实现

基于.Net平台应用程序唯一运行实例实现发布日期: 2006-06-30 | 更新日期: 2006-07-04作者:郑佐适用于: Windows 操作系统 .NET Framework 1.x,2.0...

基于.Net平台应用程序唯一运行实例实现

在开发一些应用系统的时候,由于程序内在的一些特征,系统的某些组成子程序只允许运行一个应用程序实例,以保证业务和数据处理安全。本文将从实际应用角度来分析其实现原理,对三种实现方式进行测试比较,从而确定一种合适的实现方法。文章的例子使用C#语言进行描述。 进程匹配对于每一个应用程序运行实例都会包含该...
  • isiqi
  • isiqi
  • 2007-09-10 11:04
  • 314

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

基于.Net平台应用程序唯一运行实例实现

基于.Net平台应用程序唯一运行实例实现  .NET Windows开发摘要:本文阐述了在基于.NET平台的应用程序开发中如何实现唯一应用程序运行实例,对几种实现方式进行分析测试比较,从而寻找一种合适...

VS C# 2008里只让应用程序只运行一个实例

using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;using Microsoft.Win32;using System.Diagnostics; namespace M...

使用 C# 和 C++.NET 开发的 .NET 应用程序实例列表

---------------------------------- 概述 本文档列出了 Crystal Decisions 技术支持网站上所有可用的,使用 C# 和 C++.NET 开发的 .NET 应用程序实例列表。本文档还给出了每一个程序的描述和下载链接。随着新程序加入我们的支持站点,本文...
  • isiqi
  • isiqi
  • 2004-09-25 20:46
  • 349
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)