检查WPF应用程序的其他实例是否正在/没有运行[duplicate]

时间:2023-01-18 19:41:58

This question already has an answer here:

这个问题已经有了答案:

Is it possible for a WPF application to check if any other instances of the application are running? I'm creating an application that should only have one instance, and will prompt a message that "another instance is running" when the user tries to open it again.

WPF应用程序是否可以检查应用程序的其他实例是否正在运行?我正在创建一个应该只有一个实例的应用程序,当用户试图再次打开它时,我将提示一条消息“另一个实例正在运行”。

I'm guessing I'd have to check through the process logs to match my application's name, but I'm not sure how to go about doing that.

我想我必须检查一下进程日志以匹配我的应用程序的名称,但是我不知道该怎么做。

1 个解决方案

#1


7  

The get processes by name strategy can fail if the exe has been copied and renamed. Debugging can also be problematic because .vshost is appended to the process name.

如果已复制并重命名exe,则按名称获取进程策略可能会失败。调试也可能有问题,因为.vshost被附加到进程名。

To create a single instance application in WPF, you can start by removing the StartupUri attribute from the App.Xaml file so that it looks like this...

要在WPF中创建一个实例应用程序,您可以从App.Xaml文件中删除StartupUri属性,使其看起来像这样……

<Application x:Class="SingleInstance.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Application>

After that, you can go to the App.xaml.cs file and change it so it looks like this...

然后,您可以转到App.xaml。cs文件,然后修改成这样……

public partial class App 
{
    // give the mutex a  unique name
    private const string MutexName = "##||ThisApp||##";
    // declare the mutex
    private readonly Mutex _mutex;
    // overload the constructor
    bool createdNew;
    public App() 
    {
        // overloaded mutex constructor which outs a boolean
        // telling if the mutex is new or not.
        // see http://msdn.microsoft.com/en-us/library/System.Threading.Mutex.aspx
        _mutex = new Mutex(true, MutexName, out createdNew);
        if (!createdNew)
        {
            // if the mutex already exists, notify and quit
            MessageBox.Show("This program is already running");
            Application.Current.Shutdown(0);
        }
    }
    protected override void OnStartup(StartupEventArgs e)
    {
        if (!createdNew) return;
        // overload the OnStartup so that the main window 
        // is constructed and visible
        MainWindow mw = new MainWindow();
        mw.Show();
    }
}

This will test if the mutex exists and if it does exist, the app will display a message and quit. Otherwise the application will be constructed and the OnStartup override will be called.

这将测试互斥对象是否存在,如果存在,应用程序将显示一条消息并退出。否则将构造应用程序并调用OnStartup覆盖。

Depending upon your version of Windows, raising the message box will also push the existing instance to the top of the Z order. If not you can ask another question about bringing a window to the top.

根据您的Windows版本,提高消息框也会将现有的实例推到Z顺序的顶部。如果没有,你可以问另一个问题,关于把窗口带到顶部。

There are additional features in the Win32Api that will help further customize the behaviour.

Win32Api中还有一些其他特性可以帮助进一步定制行为。

This approach gives you the message notification you were after and assures that only one instance of the main window is ever created.

此方法将提供您正在跟踪的消息通知,并确保只创建了主窗口的一个实例。

#1


7  

The get processes by name strategy can fail if the exe has been copied and renamed. Debugging can also be problematic because .vshost is appended to the process name.

如果已复制并重命名exe,则按名称获取进程策略可能会失败。调试也可能有问题,因为.vshost被附加到进程名。

To create a single instance application in WPF, you can start by removing the StartupUri attribute from the App.Xaml file so that it looks like this...

要在WPF中创建一个实例应用程序,您可以从App.Xaml文件中删除StartupUri属性,使其看起来像这样……

<Application x:Class="SingleInstance.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Application>

After that, you can go to the App.xaml.cs file and change it so it looks like this...

然后,您可以转到App.xaml。cs文件,然后修改成这样……

public partial class App 
{
    // give the mutex a  unique name
    private const string MutexName = "##||ThisApp||##";
    // declare the mutex
    private readonly Mutex _mutex;
    // overload the constructor
    bool createdNew;
    public App() 
    {
        // overloaded mutex constructor which outs a boolean
        // telling if the mutex is new or not.
        // see http://msdn.microsoft.com/en-us/library/System.Threading.Mutex.aspx
        _mutex = new Mutex(true, MutexName, out createdNew);
        if (!createdNew)
        {
            // if the mutex already exists, notify and quit
            MessageBox.Show("This program is already running");
            Application.Current.Shutdown(0);
        }
    }
    protected override void OnStartup(StartupEventArgs e)
    {
        if (!createdNew) return;
        // overload the OnStartup so that the main window 
        // is constructed and visible
        MainWindow mw = new MainWindow();
        mw.Show();
    }
}

This will test if the mutex exists and if it does exist, the app will display a message and quit. Otherwise the application will be constructed and the OnStartup override will be called.

这将测试互斥对象是否存在,如果存在,应用程序将显示一条消息并退出。否则将构造应用程序并调用OnStartup覆盖。

Depending upon your version of Windows, raising the message box will also push the existing instance to the top of the Z order. If not you can ask another question about bringing a window to the top.

根据您的Windows版本,提高消息框也会将现有的实例推到Z顺序的顶部。如果没有,你可以问另一个问题,关于把窗口带到顶部。

There are additional features in the Win32Api that will help further customize the behaviour.

Win32Api中还有一些其他特性可以帮助进一步定制行为。

This approach gives you the message notification you were after and assures that only one instance of the main window is ever created.

此方法将提供您正在跟踪的消息通知,并确保只创建了主窗口的一个实例。