谢谢

解决方案 »

  1.   

    static void Main() 
    {
    string procName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;if((System.Diagnostics.Process.GetProcessesByName(procName)).GetUpperBound(0) >0)
    {MessageBox.Show("系统已经在运行中...","警告",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Asterisk);
    }
    else
    Application.Run(new FormMain());
    }
      

  2.   

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    System.Diagnostics.Process[] process = null;
    process = System.Diagnostics.Process.GetProcessesByName("WindowsApp");
    if (process.Length > 1)
    {
    System.Diagnostics.Process.GetCurrentProcess().Kill();
    }
    Application.Run(new MainForm());
    }
      

  3.   

    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;
    }
      

  4.   

    ljleager(想飞飞不高,天天学鸟叫) 写的非常好啊。
      

  5.   

    using System;
    using System.Threading;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using JadeSoft.Common;
    namespace LogisticsSystem
    {
    /// <summary>
    /// 使应用程序只能运行一个实例 的摘要说明。
    /// </summary>
    public class AppSingleton
    {
    static Mutex m_Mutex; public static void Run()
    {
    if(IsFirstInstance())
    {
    Application.ApplicationExit += new EventHandler(OnExit);
    Application.Run();
    }

    }
    public static void Run(ApplicationContext context)
    {
    if(IsFirstInstance())
    {
    Application.ApplicationExit += new EventHandler(OnExit);
    Application.Run(context);
    }
    }
    public static void Run(Form mainForm)
    {
    if(IsFirstInstance())
    {

    Application.ApplicationExit += new EventHandler(OnExit);

    Application.Run(mainForm);
    }
    else
    {

    MessageBox.Show("应用程序已启动,请在任务管理中关闭后再启动!","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
    }
    }
    static bool IsFirstInstance()
    {
    return true;
    m_Mutex = new Mutex(false,"SingletonApp Mutext");
    bool owned = false;
    owned = m_Mutex.WaitOne(TimeSpan.Zero,false);
    return owned ;
    }
    static void OnExit(object sender,EventArgs args)
    {
    m_Mutex.ReleaseMutex();
    m_Mutex.Close();
    }
    }using System;
    using System.Drawing;
    using System.Windows.Forms;
    namespace LogisticsSystem
    {
    /// <summary>
    /// 应用程序主入口点。
    /// </summary>
    public class MainClass
    {
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    AppSingleton.Run(new mainForm());
    }
    }
    }
      

  6.   

    static bool IsFirstInstance()
    {
     //return true; 这一行注销掉。
    m_Mutex = new Mutex(false,"SingletonApp Mutext");
    bool owned = false;
    owned = m_Mutex.WaitOne(TimeSpan.Zero,false);
    return owned ;
    }
      

  7.   

    greystar(greystar) 才是正解。。前面的那些朋友,一旦出现进程名一样的不同程序,将导致第二个程序不能启动。。
      

  8.   

    同roydu(肚皮)的,用singleton模式实现是比较标准的解决办法:public class Spooler {
        private static bool instance_flag = false;    private Spooler(){
        }    public static Spooler getSpooler() {
            if(!instance_flag)
                return new Spooler();
            else
                return null;
        }
    }