如何运行ac#WinForm应用程序的一个实例?
我有一个C#应用程序,它在启动时显示登录表单,并在用户通过身份验证后显示主窗体。 我使用Mutex来限制我的应用程序只运行一个实例。 而且,这只适用于登录表单。 一旦显示主窗体,它不会限制用户重新打开登录窗体。 我一直在寻找一种解决方案,一旦主窗体已经打开,登录屏幕无法显示。
这是我的Program.cs
 [STAThread]
    static void Main()
    {
        bool mutexCreated=true;
        using (Mutex mutex = new Mutex(true, "eCS", out mutexCreated))
        {
            if (mutexCreated)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Login());
            }
            else
            {
                Process current = Process.GetCurrentProcess();
                foreach (Process process in Process.GetProcessesByName(current.ProcessName))
                {
                    if (process.Id != current.Id)
                    {
                        XtraMessageBox.Show("Another instance of eCS is already running.", "eCS already running", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        SetForegroundWindow(process.MainWindowHandle);
                        break;
                    }
                }
            }
        }
    }
我做了一些小的改变:
namespace CSMutex
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            bool mutexCreated=true;
            using(Mutex mutex = new Mutex(true, "eCS", out mutexCreated))
            {
                if (mutexCreated)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Login loging = new Login();
                    Application.Run(loging);
                    Application.Run(new Main() { UserName = loging.UserName });
                }
                else
                {
                    Process current = Process.GetCurrentProcess();
                    foreach (Process process in Process.GetProcessesByName(current.ProcessName))
                    {
                        if (process.Id != current.Id)
                        {
                            MessageBox.Show("Another instance of eCS is already running.", "eCS already running", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            //SetForegroundWindow(process.MainWindowHandle);
                            break;
                        }
                    }
                }
            }
        }
    }
}
  这可以按预期工作 - 即使当Login表单关闭(并且主应用程序表单已启动)时,也不会让用户再次运行应用程序。  我决定不建立Main从内部Login (这是我相信你如何应用工程),而是我传递参数给Main 。  我也做了一些小改动Login所以它具有UserName PROPERT(同Main )。 
如果您对Microsoft.VisualBasic的引用可以,您可以使用它的SingleInstance处理。
    [STAThread]
    static void Main(string[] args)
    {
        using (System.Threading.Mutex mutex = new System.Threading.Mutex(true, "MyApp.SingleInstance.Mutex", out createdNew))
        {
            MainForm = new MainDlg();
            SingleInstanceApplication.Run(MainForm, StartupNextInstanceEventHandler);
        }
    }
    public static void StartupNextInstanceEventHandler(object sender, StartupNextInstanceEventArgs e)
    {
        MainForm.Activate();
    }
public class SingleInstanceApplication : WindowsFormsApplicationBase
{
    private SingleInstanceApplication()
    {
        base.IsSingleInstance = true;
    }
    public static void Run(Form f, StartupNextInstanceEventHandler startupHandler)
    {
        SingleInstanceApplication app = new SingleInstanceApplication();
        app.MainForm = f;
        app.StartupNextInstance += startupHandler;
        app.Run(Environment.GetCommandLineArgs());
    }
}
  如果你只想限制一个Form一个实例,你可以这样做: 
public static class LoginForm 
{
    private static Form _loginForm = new Form();
    public static bool ShowLoginForm(){
        if(_loginForm.Visible)
             return false;
        _loginForm.Show();
        return true;
    }
}
因此,如果多于一个客户端将调用此方法,这是唯一可能的方法来显示登录表单,如果它已经可见,它将不会被执行。
链接地址: http://www.djcxy.com/p/51167.html