Winform应用程序,怎么处理,使其只启动一个边exe.如果,再次点击运行,会显示“程序已运行”。

解决方案 »

  1.   


    using   System; 
    using   System.Runtime.InteropServices; 
    using   System.Windows.Forms; 
    using   System.Diagnostics; 
    using   System.Reflection;   public   class   OneInstnace 
      {   
      [STAThread] 
      public   static   void   Main() 
      { 
      //Get   the   running   instance. 
      Process   instance   =   RunningInstance(); 
      if   (instance   ==   null) 
      { 
      //There   isn 't   another   instance,   show   our   form. 
      Application.Run   (new   Form()); 
      } 
      else 
      { 
      //There   is   another   instance   of   this   process. 
      HandleRunningInstance(instance); 
      } 
      } 
      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; 
      } 
      public   static   void   HandleRunningInstance(Process   instance) 
      { 
      //Make   sure   the   window   is   not   minimized   or   maximized 
      ShowWindowAsync   (instance.MainWindowHandle   ,   WS_SHOWNORMAL); 
      //Set   the   real   intance   to   foreground   window 
      SetForegroundWindow   (instance.MainWindowHandle); 
      } 
      [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; 
    }
      

  2.   

    或者
    //   Refer:   [C#]public   Mutex(bool   initiallyOwned,   string   name,   
    //                                                                                               out   bool   createdNew) 
    static   void   Main()   

    /*   Assure   single   instance:   */ 
    bool   isFirst; 
    System.Threading.Mutex   mtx   =   new   System.Threading.Mutex 
                                                                    (false,   "mango.2004-01-14 ",   out   isFirst); 
    if   (isFirst) 

    Application.Run(new   Form1()); 


      

  3.   

    在进程已经 运行了 同样的exe文件了找到这个exe文件并且杀掉在运行
      

  4.   

    初学者:
        process[] 获得所有进程
        进程名相等`````则messboxshow我都不好意思 发出来
      

  5.   

    to evilant,我现在出现的问题是,也是多用户登录的情况,不互斥。我的程序是放在Windows server 2003上的,除了当前用户,还时不时的有远程用户通过远程桌面登录。以至于,会一个程序在同一个电脑上能运行多次。
    还有没有别的办法?
      

  6.   

    to xuexiaodong2009,写文件或注册表。用户有时候,在进程中直接干掉程序。即未在文件或注册表,标记唯一程序已退出。这样,以后程序再也启动不起来了。(因为程序通过标记判断,认为已经启动。)
      

  7.   

    互斥体是操作系统 提供的一种现成模式。楼主为啥不自己想想办法呢?
    例如:
     在program里还没run的时候,搜索所有的激活状态窗口,找到自己那个了就给他发消息,让那个窗口弹个框"老子我已经打开了"然后关闭程序。要是找不到就说明没有,那就启动呗。    可以监听一个本地网络端口,然后开启的时候再去监听,会得到已经被占用,然后用client端发消息问:你是我自己嘛? 你自己的server就会:是。 然后关闭。
          当然如果你监听成功了,那就开启呗。      只看你敢不敢想。 
      

  8.   

    tryProcess[] pary = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
    (Application_ThreadException);       if (pary.Length > 1)
    {
         MessageBox.Show("已经有相同的程序在运行!");
         return;
    }
      

  9.   

    using   System.Diagnostics;
      

  10.   

    using System.Diagnostics;Process[] pary = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
    if (pary.Length > 1)
    {
         MessageBox.Show("已经有相同的程序在运行!");
         return;
    }
      

  11.   

    在构造函数中调用即可
    /// <summary>
            /// 单实例设置
            /// </summary>
            private void MutexRun()
            {
                bool bRun;
                m_mt = new Mutex(true, "TestRun", out bRun);
                if (!bRun)
                {
                    IntPtr Hander = CommonGenerator.FindWindow(null, "窗口标题");
                    if (Hander != IntPtr.Zero)
                    {
                        // 在这里提示用户,同时将唯一的窗口置为最前方
                        CommonGenerator.SetForegroundWindow(Hander);
                    }
                    m_mt.Close();
                    Environment.Exit(1);
                    return;
                }
            }
      

  12.   

    楼主 这些代码baidu 即可,不值得问把
      

  13.   

      to dongxinxi, isjoe: 前面已经提到,“互斥”,对对用户是不管使的!
      

  14.   

    后边几个人的方法和前边一样啊,那些都解决不了啊,我现在出现的问题是,也是多用户登录的情况,不互斥。我的程序是放在Windows server 2003上的,除了当前用户,还时不时的有远程用户通过远程桌面登录。以至于,会一个程序在同一个电脑上能运行多次。
      

  15.   

    写文件,或者注册表,每次启动时判断
    加上是不是一种方式呢
    Process[] pary = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)一种思路罢了,不是简单的互斥可以解决的
      

  16.   

    要用全局互斥。    bool bRun;
        m_mt = new Mutex(true, "Global\\TestRun", out bRun);
        if (!bRun)
      

  17.   

    to dongxinxi, isjoe: 前面已经提到,“互斥”,对多用户是不管使的!
      

  18.   


    /// <summary>
            /// 检测程序是否已经运行
            /// </summary>
            /// <returns>bool true:未运行 false:已经运行</returns>
            static bool IsRun()
            {
                Process[] prs = Process.GetProcesses();
                Process thisone = Process.GetCurrentProcess();
                foreach (Process pr in prs)
                {
                    if (thisone.ProcessName == pr.ProcessName && thisone.Id != pr.Id)
                    {
                        return false;
                    }
                }
                return true;
            }
    这样不会的
      

  19.   

            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(true);
                mutex = new Mutex(true, "hygl");
                if (mutex.WaitOne(0, false))
                {
                    
                }
                else
                {
                    MessageBox.Show("程序已经运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Application.Exit();
                }
            }
      

  20.   


    创建一个全局的mutex,就可以解决多用户登录的问题。
    前缀 "Global\\"
    private static System.Threading.Mutex _mutex;
    [STAThread]
    static void Main()
    {
        _mutex = new System.Threading.Mutex(false, "Global\\MyApplication");
        //Mutex所有权取得
        if (_mutex.WaitOne(0, false) == false)
        {
            MessageBox.Show("应用程序已经启动过了。");
            return;
        }
        Application.Run(new Form1());
    }
      

  21.   


    哦!gomoku 已经说了~ 
      

  22.   

    还是这样的好啊private static System.Threading.Mutex _mutex;
    [STAThread]
    static void Main()
    {
        _mutex = new System.Threading.Mutex(false, "Global\\MyApplication");
        //Mutex所有权取得
        if (_mutex.WaitOne(0, false) == false)
        {
            MessageBox.Show("应用程序已经启动过了。");
            return;
        }
        Application.Run(new Form1());
    }
      

  23.   

    每用户只能运行一个程序
    System.Threading.Mutex run = new System.Threading.Mutex(true, Environment.UserName+"single84123131", out runone);
    if (runone)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());                run.ReleaseMutex(); //关键
                }
                else
                {
                    //MessageBox.Show("已经运行一个实例!");            }