C#winforms如何禁止同一个程序运行两次?

解决方案 »

  1.   

    使用内核对象Mutex可以防止同一个进程运行两次。注意:是名称相同的进程,而不是exe,因为exe程序可以改名。
    bool canCreateNew;
    m_ServerMutex = new Mutex(true, "EMTASS_SERVER", out canCreateNew);
    if (!canCreateNew)
    {
       throw new Exception("Can create two or more server!");
    }
      

  2.   

    可以判断是不是已启动该进程,大概方法如下:
            System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcesses();
            foreach (System.Diagnostics.Process myProcess in myProcesses)
            {
              if ("进程名" == myProcess.ProcessName)
                  // 处理办法             
            }
      

  3.   

    修改Program.cs 文件  /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                System.Diagnostics.Process _RunProcess = System.Diagnostics.Process.GetCurrentProcess();
                System.Diagnostics.Process[] _Process = System.Diagnostics.Process.GetProcessesByName(_RunProcess.ProcessName);            if (_Process.Length == 1)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
                }
                else
                {
                    //重复提示
                }
            }
      

  4.   


    [STAThread]
    public static void Main()
    {
        bool ret;
        System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);
        if (ret)
        {
            System.Windows.Forms.Application.EnableVisualStyles();   //这两行实现   XP   可视风格   
             System.Windows.Forms.Application.DoEvents();
            //   Main   为你程序的主窗体,如果是控制台程序不用这句
             System.Windows.Forms.Application.Run(new Main());   
             mutex.ReleaseMutex();
        }
        else
        {
            MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //   提示信息,可以删除。   
            Application.Exit();//退出程序   
        }
    }
      

  5.   


    Process[] myProcesses; 
            myProcesses = Process.GetProcessesByName("NotePad.exe"); 
            if (myProcesses.Length == 0) 
            { 
                p = Process.Start"NotePad.exe"); 
            } 
      

  6.   

    using System.Threading;mutex.ReleaseMutex; // 这个地方报错;mutex : 
    错误 只有 assignment、call、increment、decrement 和 new 对象表达式可用作语句
      

  7.   

    最简单的方法, 调用API函数
      

  8.   


    "mutex.ReleaseMutex; // 这个地方报错;"
    我在5楼给你的代码中哪里用到“mutex.ReleaseMutex”?
      

  9.   

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.OracleClient;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Configuration;
    using System.IO;    string name = Process.GetCurrentProcess().ProcessName;
        int id = Process.GetCurrentProcess().Id;
        Process[] prc = Process.GetProcesses();
        foreach (Process pr in prc)
        {
           if ((name == pr.ProcessName) && (pr.Id != id))
          {
               MessageBox.Show("对不起,本地已经有该系统正在运行!\n ", "提示 ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
               System.Environment.Exit(0);
          }
       }
      

  10.   

    麻烦5楼的再看一下5楼的内容。    if (ret)
        {
            System.Windows.Forms.Application.EnableVisualStyles();   //这两行实现   XP   可视风格   
             System.Windows.Forms.Application.DoEvents();
            //   Main   为你程序的主窗体,如果是控制台程序不用这句
             System.Windows.Forms.Application.Run(new Main());   
             mutex.ReleaseMutex();
        }
        else
        {
            MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //   提示信息,可以删除。   
            Application.Exit();//退出程序   
        }
      

  11.   


    呵呵,不好意思,没看仔细是用到的。
    但没有问题啊,给你个全部代码看看吧using System;
    using System.Collections.Generic;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                bool ret;
                System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);
                if (ret)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
                    mutex.ReleaseMutex();
                }
                else
                {
                    MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    //   提示信息,可以删除。   
                    Application.Exit();//退出程序   
                }
            }
        }
    }
      

  12.   

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Threading;namespace FallingGold
    {
        static class Program
        {
            private static Mutex mutex;
            /// 
            /// 应用程序的主入口点。
            /// 
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                mutex = new Mutex(true, "一个");            
                //保证只有一个实例运行
                if (mutex.WaitOne(0, false))
                {
                    Application.Run(new Form1());
                }
            }
        }
    }