mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 19:26:39 +02:00
Add one instance support.
This commit is contained in:
@@ -1,28 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
using Microsoft.VisualBasic.ApplicationServices;
|
||||
using StartupEventArgs = System.Windows.StartupEventArgs;
|
||||
|
||||
namespace WinAlfred
|
||||
{
|
||||
/// <summary>
|
||||
/// App.xaml 的交互逻辑
|
||||
/// </summary>
|
||||
public static class EntryPoint
|
||||
{
|
||||
[STAThread]
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
SingleInstanceManager manager = new SingleInstanceManager();
|
||||
manager.Run(args);
|
||||
}
|
||||
}
|
||||
|
||||
// Using VB bits to detect single instances and process accordingly:
|
||||
// * OnStartup is fired when the first instance loads
|
||||
// * OnStartupNextInstance is fired when the application is re-run again
|
||||
// NOTE: it is redirected to this instance thanks to IsSingleInstance
|
||||
public class SingleInstanceManager : WindowsFormsApplicationBase
|
||||
{
|
||||
App app;
|
||||
|
||||
public SingleInstanceManager()
|
||||
{
|
||||
this.IsSingleInstance = true;
|
||||
}
|
||||
|
||||
protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
|
||||
{
|
||||
// First time app is launched
|
||||
app = new App();
|
||||
app.InitializeComponent();
|
||||
app.Run();
|
||||
return true;
|
||||
}
|
||||
|
||||
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
|
||||
{
|
||||
// Subsequent launches
|
||||
base.OnStartupNextInstance(eventArgs);
|
||||
app.Activate(eventArgs.CommandLine.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
public partial class App : Application
|
||||
{
|
||||
private MainWindow window;
|
||||
|
||||
protected override void OnStartup(StartupEventArgs e)
|
||||
{
|
||||
base.OnStartup(e);
|
||||
bool startupFlag;
|
||||
Mutex mutex = new Mutex(true, "WinAlfred", out startupFlag);
|
||||
if (!startupFlag)
|
||||
{
|
||||
Environment.Exit(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
MainWindow mainWindow = new MainWindow();
|
||||
mainWindow.Show();
|
||||
}
|
||||
|
||||
window = new MainWindow();
|
||||
window.ShowApp(e.Args);
|
||||
}
|
||||
|
||||
public void Activate(string[] commandLine)
|
||||
{
|
||||
window.ShowApp(commandLine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user