如何实现只让一个程序运行一次,在它运行时,不允许再启动一个新的进程。我的系统是WINDOWS2003,用的环境是。NET 2003, VC#

解决方案 »

  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()
      {
        //得到正在运行的例程
        Process instance = RunningInstance();
        if (instance == null)
        {
          //如果没有其它例程,就新建一个窗体
          Application.Run (new Form());
        }
        else
        {
          //处理发现的例程
          HandleRunningInstance(instance);
        }
      }
      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 (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
              current.MainModule.FileName)
            {
              //返回另一个例程实例
              return process;
            }
          }
        }    //没有其它的例程,返回Null
        return null;
      }  public static void HandleRunningInstance(Process instance)
      {
        //确保窗口没有被最小化或最大化
        ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);    //设置真实例程为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.   

    Mutex mutex = new Mutex(false, "ThisShouldOnlyRunOnce");
       //判断互斥体是否使用中。
       bool Running = !mutex.WaitOne(0, false);
       if (! Running) 
       {
        Application.Run(new Form1());
       }
       else   
       {
        MessageBox.Show("应用程序已经启动!");
       }
      

  3.   

    我收藏的一个:
    http://blog.csdn.net/dahuzizyd/articles/55411.aspx
      

  4.   

    [STAThread]
    static void Main() 
    {

    int ProceedingCount = 0;
    Process[] ProceddingCon = Process.GetProcesses();
    foreach(Process IsProcedding in ProceddingCon) {
    if(IsProcedding.ProcessName == Process.GetCurrentProcess().ProcessName) {
    ProceedingCount += 1;
    }
    }
    if(ProceedingCount > 1) {
    MessageBox.Show("该系统已经在运行中。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
    }
    else {
    Application.EnableVisualStyles();
    Application.DoEvents();
    Application.Run(new MainFrm());
    }
              }
      

  5.   

    经测试chinawn(chinawin) 的方法在启动应用程序时若鼠标点击比较快,会出现两个启动的同一进程互锁,导致提示两个“该系统已经在运行中”的错误。因为以前我也是这么做的。
      

  6.   

    可以使用互斥体Mutex类型完成此功能。见如下代码:
        [STAThread]
        public static void Main(string[] args) 
        {
             //声明互斥体。
             Mutex mutex = new Mutex(false, "ThisShouldOnlyRunOnce");
             //判断互斥体是否使用中。
             bool Running = !mutex.WaitOne(0, false);
             if (! Running)
                 Application.Run(new FormLogin());
             else
                 MessageBox.Show("应用程序已经启动!");
        }