检测,如果应用程序的另一实例已经在运行 [英] Detecting if another instance of the application is already running

查看:1201
本文介绍了检测,如果应用程序的另一实例已经在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要的行为稍有不同,当它加载如果已经有运行的实例。



我知道如何使用互斥体,防止其他实例加载,但。这并不完全解决我的问题。



例如:




  • 实例1载荷,获取互斥。

  • 实例2负荷,无法获得互斥体,知道有另一个实例。到目前为止,一切都很好。

  • 实例1闭合,释放互斥。

  • 实例3载荷,获取互斥体,不知道该实例2仍在运行。



任何想法?值得庆幸的是它并不需要处理多个用户帐户或类似的东西。



(C#,桌面应用程序)



编辑:为了澄清,该应用程序不需要被限制在一个实例中,只需稍微不同的启动动作,如果有已经运行的另一个实例。多个实例的罚款(和预期)。


解决方案

这可能会做什么您想要

。它具有使已经运行的实例向前不错的附加功能



编辑:修改代码来自动确定应用程序的标题

 使用系统;使用System.Diagnostics程序
;
使用System.Linq的;
使用的System.Reflection;使用System.Runtime.InteropServices
;

静态无效的主要()
{
如果
{
回报(EnsureSingleInstance()!);
}

// ...
}

静态布尔EnsureSingleInstance()
{
过程currentProcess =进程。 GetCurrentProcess();

VAR runningProcess =(从Process.GetProcesses()过程
,其中
process.Id = currentProcess.Id和放大器;!&安培;
process.ProcessName.Equals (
currentProcess.ProcessName,
StringComparison.Ordinal)
选择过程).FirstOrDefault();

如果(runningProcess!= NULL)
{
的ShowWindow(runningProcess.MainWindowHandle,SW_SHOWMAXIMIZED);
SetForegroundWindow(runningProcess.MainWindowHandle);

返回false;
}

返回真;
}

函数[DllImport(user32.dll中,入口点=SetForegroundWindow)]
私人静态的extern BOOL SetForegroundWindow(IntPtr的的hWnd);

函数[DllImport(user32.dll中)]
私人静态外部布尔的ShowWindow(IntPtr的的HWND,的Int32的nCmdShow);

私人const int的SW_SHOWMAXIMIZED = 3;


My application needs to behave slightly differently when it loads if there is already an instance running.

I understand how to use a mutex to prevent additional instances loading, but that doesn't quite solve my problem.

For example:

  • Instance 1 loads, gets the mutex.
  • Instance 2 loads, can't get the mutex, knows there's another instance. So far, so good.
  • Instance 1 closes, releases the mutex.
  • Instance 3 loads, gets the mutex, doesn't know that Instance 2 is still running.

Any ideas? Thankfully it doesn't need to deal with multiple user accounts or anything like that.

(C#, desktop application)

Edit: To clarify, the application doesn't need to be restricted to a single instance, just perform a slightly different start-up action if there's another instance already running. Multiple instances are fine (and expected).

解决方案

This will probably do just what you want. It has the nice additional feature of bringing the already running instance forward.

EDIT: updated the code to determine the application title automatically.

using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

static void Main()
{
    if (!EnsureSingleInstance())
    {
        return;
    }

    //...
}

static bool EnsureSingleInstance()
{
    Process currentProcess = Process.GetCurrentProcess();

    var runningProcess = (from process in Process.GetProcesses()
                          where
                            process.Id != currentProcess.Id &&
                            process.ProcessName.Equals(
                              currentProcess.ProcessName,
                              StringComparison.Ordinal)
                          select process).FirstOrDefault();

    if (runningProcess != null)
    {
        ShowWindow(runningProcess.MainWindowHandle, SW_SHOWMAXIMIZED);
        SetForegroundWindow(runningProcess.MainWindowHandle);

        return false;
    }

    return true;
}

[DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);

private const int SW_SHOWMAXIMIZED = 3;

这篇关于检测,如果应用程序的另一实例已经在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆