C# 启动单个实例

 先说下我详细的几点需求(假设程序名为"test.exe")

  1.程序只能同时打开一个实例.

  2.在test.exe已经启动的情况下,双击A.exe,则把已经启动的test.exe激活,并呈现到最前.

  3.复制test.exe,命名为B.exe,在test2.exe已经启动的情况下,双击test2.exe,则把test.exe激活,并呈现到最前.

  好,现在就来看看网络上的解决方案

  1.互斥法

using System;
using System.Windows.Forms;
using System.Threading;

namespace NSUI
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        public static void Main()
        {
            bool createdNew;
            Mutex instance = new Mutex(true, "互斥名(保证在本机中唯一)", out  createdNew);

            if (createdNew)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new FormLogin());

                instance.ReleaseMutex();
            }
            else
            {
                MessageBox.Show("已经启动了一个程序的实例,请先退出!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();

            }
        }
    }
}

  评价:

  个人认为这种方法非常的好,能做出判断的准确,即使启动复制的执行文件,依然可以提示"已经启动一个程序,请先退出!".这样,它满足了上述需要中的第一条和第三条的前半部分.但是有一个不足:无法激活已经启动的程序(至少我不知道怎么实现 ,如果有谁知道用互斥可以实现以上三个要求,请留言告诉我,不胜感激!)

  2.Process法

  添加如下函数:

  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;

  }

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

  if( RunningInstance() == null )

  Application.Run(new yourFormName());

  评价:速度比较慢,其次通过ProcessName去系统中查寻,有可能查出来的Process并不是我想要得,不过,稍做修改,便可以很实现需求的第二条(让程序只运行一次,如果程序已经运行,把它弹出并显示到最前面).但是它同样有一个很严重的问题,也就无法满足需求中的第三条,做一个复制,然后修改名字(程序名即为进程名),便可以启动多个实例.

  3.VB法(个人推荐的方法,谈不上原创,但是网络上很少见)

  不解释,直接看代码

  using

  Microsoft.VisualBasic.ApplicationServices;

  static void Main(string[] args)

  {

  App myApp = new App();

  myApp.Run(args);

  }

  class App : WindowsFormsApplicationBase

  {

  public App()

  {

  // 设置单例模式

  this.IsSingleInstance = true;

  // 设置可用于XP窗口样式

  this.EnableVisualStyles = true;

  // 窗口关闭时的操作

  this.ShutdownStyle = ShutdownMode.AfterMainFormCloses;

  }

  /// <summary>

  /// 重写OnCreateMainForm()函数

  /// </summary>

  protected override void OnCreateMainForm()

  {

  this.MainForm = new FormMain();

  }

  }

  }

  怎么样,也不是很复杂,代码量很少,轻松实现所有需求.当然,有些朋友还可能还有这样的需求

  程序第二次启动的时候,除了把程序激活前置,还要往程序里传递参数,并做处理.没有问题,这样也可以做到,但是可能稍微复杂一点.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值