Kill PowerToys Run process if it is already running (#11407)

This commit is contained in:
Mykhailo Pylyp
2021-05-24 12:05:17 +03:00
committed by GitHub
parent 0be526bd7e
commit d537280454
5 changed files with 88 additions and 40 deletions

View File

@@ -11,6 +11,7 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using interop;
// http://blogs.microsoft.co.il/arik/2010/05/28/wpf-single-instance-application/
// modified to allow single instance restart
@@ -44,11 +45,6 @@ namespace PowerLauncher.Helper
/// </summary>
private const string ChannelNameSuffix = "SingeInstanceIPCChannel";
/// <summary>
/// Prefix to the names of mutexes which ensures they are unique in a Windows session.
/// </summary>
private const string LocalMutexPrefix = @"Local\";
/// <summary>
/// Gets or sets application mutex.
/// </summary>
@@ -59,15 +55,15 @@ namespace PowerLauncher.Helper
/// If not, activates the first instance.
/// </summary>
/// <returns>True if this is the first instance of the application.</returns>
internal static bool InitializeAsFirstInstance(string uniqueName)
internal static bool InitializeAsFirstInstance()
{
string mutexName = @"Local\PowerToys_Run_InstanceMutex";
// Build unique application Id and the IPC channel name.
string applicationIdentifier = uniqueName + Environment.UserName;
string applicationIdentifier = mutexName + Environment.UserName;
string channelName = string.Concat(applicationIdentifier, Delimiter, ChannelNameSuffix);
// Create mutex based on unique application Id to check if this is the first instance of the application.
string mutexName = string.Concat(LocalMutexPrefix, uniqueName);
SingleInstanceMutex = new Mutex(true, mutexName, out bool firstInstance);
if (firstInstance)
{