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("应用程序已经启动!");
    }

解决方案 »

  1.   

    //判断是否已经存在一个
    bool createdNew;
    Mutex m = new Mutex(true, "yourname", out createdNew);
    if (! createdNew)
    {
    MessageBox.Show("Only one name is allowed at a time.");
    return;
    }
    Application.Run(new Start());
    GC.KeepAlive(m);
      

  2.   

    public static Process RunningInstance() 

    //log
    Log("Process starting");
    //current process
    Process current = Process.GetCurrentProcess();
    //processes name
    Process[] processes = Process.GetProcessesByName (current.ProcessName); 
    foreach (Process process in processes) 

    //id compare
    if (process.Id != current.Id) 

    //path compare
    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) 

    //log
    Log("Process has benn staring");
    //Return the other process instance. 
    return process; 



    Log("Process start");
    //No other instance was found, return null. 
    return null; 

    static void Main() 
    {
    Log("Main");
    //getinstance
    if(FileWatch.RunningInstance() == null)
    {
    Application.Run(new FileWatch());
    }
    //show message
    else
    {
    //log
    Log("Application has stoped"); MessageBox.Show("error","警告",
    MessageBoxButtons.OK,MessageBoxIcon.Stop);
    }