static void Main()
        {   
     
        //    MessageBox.Show("数据库连接成功!", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            FrmLogin FrmLog = new FrmLogin();
            FrmLog.ShowDialog();
            if (FrmLogin.CanLogin == true)
            {
                bool creatnew;
                System.Threading.Mutex m = new System.Threading.Mutex(false, "ysucs", out creatnew);
                if (creatnew)
                {
                    Application.EnableVisualStyles();
                    Application.Run(new FrmMdiMain());
                    m.ReleaseMutex();
                }
                else 
                {
                    MessageBox.Show("不能同时运行两个实例~!", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    return;
                }
            }感觉代码没什么错误啊,但是还是能运行两个实例,郁闷。

解决方案 »

  1.   

    m.ReleaseMutex(); 这句应该放在Application.ApplicationExit的EventHandler里面吧,mutex放到class FrmMdiMain里面,static了吧。
      

  2.   

    我这么用过,好使啊
    不过我的Main方法里没有登录窗体的创建,所以总是可能出现在这上面,互斥类的用法和你写的一样
      

  3.   

    static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static int Main(string[] args)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                if (runone())
                Application.Run(new Form4
                    ());
            return 1;
            }
            private static System.Threading.Mutex mutex;
            private static bool runone()
            {
                bool one;
                mutex = new System.Threading.Mutex(true, "WindowsApplication2", out   one);
                return one;
            }
        }
      

  4.   

    static class Program 
        { 
            ///  <summary> 
            /// The main entry point for the application. 
            ///  </summary> 
            [STAThread] 
            static int Main(string[] args) 
            { 
                Application.EnableVisualStyles(); 
                Application.SetCompatibleTextRenderingDefault(false); 
                if (runone()) 
                Application.Run(new Form4 
                    ()); 
            return 1; 
            } 
            private static System.Threading.Mutex mutex; 
    //静态...的才可以。
            private static bool runone() 
            { 
                bool one; 
                mutex = new System.Threading.Mutex(true, "WindowsApplication2", out   one); 
                return one; 
            } 
        }
      

  5.   

    方法一:
    [STAThread]
    static void Main(string[] args) 
    {
    if (!IsRun())
    {
    System.Windows.Forms.Application.Run(new frm1());
    }
    else
    {
    System.Windows.Forms.MessageBox.Show("程序已经运行");
    }
    }
    private bool IsRun()
    {
    int i=0;
    Process[] ps=Process.GetProcesses();
    foreach(Process p in ps)
    {
    if (p.ProcessName.ToLower()=="自身程序名称(显示在系统任务列表中名称)")
    i++;
    }
    return i<2;   //包括自身
    }
      

  6.   

    方法二:
    /// <summary>
    /// Start 的摘要说明。
    /// </summary>
    public class Start
    {
    [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;  public Start()
    {
    }
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    if(!GetRunningInstance(true))
    {
    Application.Run(new frmMdi());
    }
    }
    public static bool GetRunningInstance(bool AutoActive) 

    Process currentProcess = Process.GetCurrentProcess(); //获取当前进程 
    //获取当前运行程序完全限定名 
    string currentFileName = currentProcess.MainModule.FileName; 
    //获取进程名为ProcessName的Process数组。 
    Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName); 
    //遍历有相同进程名称正在运行的进程 
    bool r=false;
    foreach (Process process in processes) 

    if (process.MainModule.FileName == currentFileName) 

    if (process.Id != currentProcess.Id) //根据进程ID排除当前进程 
    {
    r=true;
    if(AutoActive)
    HandleRunningInstance( process);//返回已运行的进程实例 
    }


    return r; 
    }
    public static bool GetRunningInstance() 

    return GetRunningInstance(false);

    public static bool HandleRunningInstance(Process instance) 

    //确保窗口没有被最小化或最大化 
    ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); 
    //设置为foreground window 
    return SetForegroundWindow(instance.MainWindowHandle); 


    }