限制应用程序的多个实例

发布于 2024-07-29 07:59:40 字数 150 浏览 6 评论 0原文

好的,我已经创建了我的 C# 应用程序,为其创建了一个安装程序,并将其安装在我的计算机上。

问题是,当用户打开应用程序exe两次时,将会有两个应用程序实例在运行。 我只希望应用程序的一个实例在任何时候运行,我该如何做到这一点?

感谢您的帮助,

Okay, so i've created my c# application, created an installer for it and have it working installed on my machine.

The problem is, when the user opens the application exe twice, there will be two instances of the application running. I only ever want one instance of the application to be running at any time, how do I go about doing this?

Thanks for your help,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

左岸枫 2024-08-05 07:59:41

常见的技术是创建一个命名的互斥体并在应用程序启动时检查它是否存在。

请参阅这个

来自 DDJ 的代码:

class App : Form
{
    Mutex mutex;

    App()
    {
        Text = "Single Instance!";
        mutex = new Mutex(false, "SINGLE_INSTANCE_MUTEX");
        if (!mutex.WaitOne(0, false)) 
        {
            mutex.Close();
            mutex = null;
        }
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
            mutex.ReleaseMutex();
        base.Dispose(disposing);
    }

    static void Main()
    {
        App app = new App();
        if (app.mutex != null) Application.Run(app);
        else MessageBox.Show("Instance already running");
    }
}

The common technique for this is to create a named Mutex and check for its presence on application start.

See this or this.

Code from DDJ:

class App : Form
{
    Mutex mutex;

    App()
    {
        Text = "Single Instance!";
        mutex = new Mutex(false, "SINGLE_INSTANCE_MUTEX");
        if (!mutex.WaitOne(0, false)) 
        {
            mutex.Close();
            mutex = null;
        }
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
            mutex.ReleaseMutex();
        base.Dispose(disposing);
    }

    static void Main()
    {
        App app = new App();
        if (app.mutex != null) Application.Run(app);
        else MessageBox.Show("Instance already running");
    }
}
眼眸里的那抹悲凉 2024-08-05 07:59:41

我通过这个解决了这个问题,

[STAThread]
 static void Main()
    {

        Process[] result = Process.GetProcessesByName("ApplicationName");
        if (result.Length > 1)
        {
            MessageBox.Show("There is already a instance running.", "Information");
            System.Environment.Exit(0);
        }
        // here normal start 
    }

这很简单,但我几乎没有时间检查更好的解决方案。

i solved this problem by this

[STAThread]
 static void Main()
    {

        Process[] result = Process.GetProcessesByName("ApplicationName");
        if (result.Length > 1)
        {
            MessageBox.Show("There is already a instance running.", "Information");
            System.Environment.Exit(0);
        }
        // here normal start 
    }

it is simple, but i had hardly time to check for better solutions.

深者入戏 2024-08-05 07:59:41

感谢先生们。 艾伦鲍威尔:

static void Main() 
{
    using (Mutex mutex = new Mutex(false, @"Global\" + appGuid)) {
        if (!mutex.WaitOne(0, false)) {
            string processName = GetProcessName();
            BringOldInstanceToFront(processName);
        }
        else {
            GC.Collect();
            Application.Run(new Voting());
        }
    }
}

private static void BringOldInstanceToFront(string processName) {
    Process[] RunningProcesses = Process.GetProcessesByName(processName);
    if (RunningProcesses.Length > 0) {
        Process runningProcess = RunningProcesses[0];
        if (runningProcess != null) {
            IntPtr mainWindowHandle = runningProcess.MainWindowHandle;
            NativeMethods.ShowWindowAsync(mainWindowHandle, (int) WindowConstants.ShowWindowConstants.SW_SHOWMINIMIZED);
        NativeMethods.ShowWindowAsync(mainWindowHandle, (int) WindowConstants.ShowWindowConstants.SW_RESTORE);
        }
    }
}

With thanks to Messrs. Allen and Powell:

static void Main() 
{
    using (Mutex mutex = new Mutex(false, @"Global\" + appGuid)) {
        if (!mutex.WaitOne(0, false)) {
            string processName = GetProcessName();
            BringOldInstanceToFront(processName);
        }
        else {
            GC.Collect();
            Application.Run(new Voting());
        }
    }
}

private static void BringOldInstanceToFront(string processName) {
    Process[] RunningProcesses = Process.GetProcessesByName(processName);
    if (RunningProcesses.Length > 0) {
        Process runningProcess = RunningProcesses[0];
        if (runningProcess != null) {
            IntPtr mainWindowHandle = runningProcess.MainWindowHandle;
            NativeMethods.ShowWindowAsync(mainWindowHandle, (int) WindowConstants.ShowWindowConstants.SW_SHOWMINIMIZED);
        NativeMethods.ShowWindowAsync(mainWindowHandle, (int) WindowConstants.ShowWindowConstants.SW_RESTORE);
        }
    }
}
病毒体 2024-08-05 07:59:41

我不知道您正在运行的环境,但关于“单实例应用程序”需要记住的是如何定义单实例。 如果应用程序可以使用通用数据源同时在多个工作站上运行,这会是一个问题吗? 同样,对于多个用户登录同一台计算机的终端服务情况(或“运行方式”情况),您是否希望以每个用户、每个用户只能运行一个实例的方式限制应用程序? -电脑? 或者您是否同意每个用户只有一个实例?

这些问题的答案可能会引导您走向一个方向而不是另一个方向。 例如,我们有一个“单实例”应用程序,其范围是一组计算机。 该组工作站中只允许一个用户使用。 我们通过在共享数据源中创建一个表来跟踪当前连接的用户来管理此操作。 这是一个维护问题,因为您需要确保该表始终 100% 准确。 处理诸如工作站意外断电、在该表中留下“虚假”记录之类的事情需要谨慎处理。

I don't know the environment that you are operating in, but something to keep in mind about 'single-instance applications' is how you define single-instance. If the application can be run on multiple workstations at the same time, using a common datasource, is that an issue? Likewise, what about a terminal-services situation (or a "run as" situation) where more than one user is logged into the same computer, do you want to restrict the application in such a way that only one instance per-user, per-computer? Or are you okay with it simply being one instance per user?

The answer to these might lead you in one direction over another. For example, we have a 'single-instance' application with the scope being a group of computers. Only one user is allowed on within that group of workstations. We managed this by have a table in our shared data-source that tracked currently connected users. This is a maintenance issue as you need to be sure that table is 100% accurate all the time. Handling things like unexpected power outages on the workstation, leaving "bogus" records in that table took some careful handling.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文