弹来弹去跑马灯!

c# 限制同时启动多个实例程序运行

 

 

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

namespace WindowsFormsApplication4
{
   public  class MultiRunChecker{
  

      public   static bool  hasMultiRun()
        {

            Process[] ps = Process.GetProcessesByName(Assembly.GetExecutingAssembly().GetName().Name);
            if (ps != null && ps.Length > 1)
            {

                //发现重复进程
                return true;
            }

            return false;

        }
    }
}


调用:
//防止改名运行
 if (!Application.ExecutablePath.EndsWith(Assembly.GetExecutingAssembly().GetName().Name+".exe")) {
                MessageBox.Show("app not valid");
                Application.Exit();
            }

            if (MultiRunChecker.hasMultiRun()) {、、防止多个程序运行

                MessageBox.Show("has");
                Application.Exit();

            }

  

posted @ 2018-09-28 15:00  wgscd  阅读(632)  评论(10编辑  收藏  举报