WinForm程序只运行一个实例

来源:互联网 发布:java分布式系统 编辑:程序博客网 时间:2024/06/14 11:14

只启动一个窗体,如果再次启动则激活该窗体 

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace TEST
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
       
        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        private const int WS_SHOWNORMAL = 1;

        [STAThread]
        static void Main()
        {
            //得到正在运行的例程
            Process instance = RunningInstance();
            if (instance == null)
            {
                //如果没有其它例程,就新建一个窗体
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else
            {
                //处理发现的例程
                HandleRunningInstance(instance);
            }
        }
        /// <summary>
        /// 得到正在运行的进程
        /// </summary>
        /// <returns></returns>
        public static Process RunningInstance()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);

            //遍历正在有相同名字运行的进程
            foreach (Process process in processes)
            {
                //忽略现有的进程
                if (process.Id != current.Id)
                {
                    //确保进程从EXE文件运行
                    if (process.MainModule.FileName == current.MainModule.FileName)
                    {
                        // 返回另一个进程实例
                        return process;
                    }
                }
            }
            //没有其它的进程,返回Null
            return null;
        }
        /// <summary>
        /// 处理正在运行的进程,也就是将其激活
        /// </summary>
        /// <param name="instance">要处理的进程</param>
        public static void HandleRunningInstance(Process instance)
        {
            //确保窗口没有被最小化或最大化
            ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
            //设置真实进程为foreground window
            SetForegroundWindow(instance.MainWindowHandle);
        }
    }
}

0 0