This is the code I implemented so far to create a single instance WPF application:
#region Using Directives
using System;
using System.Globalization;
using S
Just throwing my hat into the ring here. What I do is I create an ApplicationBase
subclass of the regular Application
class which I keep in a common library I use in all my WPF applications. Then I change the base class (from within the XAML and its code-behind) to use my base class. Finally, I use an EntryPoint.Main
as the startup object for my app, which I then check the single instance status, and simply return if I'm not the first.
Note: I also show how to support a flag that lets you override that if you want to launch another instance. However, be careful with such an option. Only use it where it makes actual sense.
Here's the code:
public abstract class ApplicationBase : Application {
public static string? SingleInstanceId { get; private set; }
public static bool InitializeAsFirstInstance(string singleInstanceId){
if(SingleInstanceId != null)
throw new AlreadyInitializedException(singleInstanceId);
SingleInstanceId = singleInstanceId;
var waitHandleName = $"SingleInstanceWaitHandle:{singleInstanceId}";
if(EventWaitHandle.TryOpenExisting(waitHandleName, out var waitHandle)){
// An existing WaitHandle was successfuly opened which means we aren't the first so signal the other
waitHandle.Set();
// Then indicate we aren't the first instance by returning false
return false;
}
// Welp, there was no existing WaitHandle with this name, so we're the first!
// Now we have to set up the EventWaitHandle in a task to listen for other attempts to launch
void taskBody(){
var singleInstanceWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, waitHandleName);
while (singleInstanceWaitHandle.WaitOne()) {
if(Current is ApplicationBase applicationBase)
Current.Dispatcher.BeginInvoke(applicationBase.OtherInstanceLaunched);
}
}
new Task(taskBody, TaskCreationOptions.LongRunning).Start();
return true;
}
public static bool IsSingleInstance
=> SingleInstanceId != null;
protected virtual void OtherInstanceLaunched()
=> Current.MainWindow?.BringToFront();
}
By marking OtherInstanceLaunched
as virtual, I can customize that on a per-application basis by simply overriding it, or just let the default implementation do its thing, which here, is an extension method on Window
that I added. (Essentially it makes sure it's visible, restored, then focuses it.)
public static class EntryPoint {
public static class CommandLineArgs{
public const string AllowMulti = "/AllowMulti";
public const string NoSplash = "/NoSplash";
}
[STAThread]
public static int Main(string[] args) {
var showSplashScreen = true;
var allowMulti = false;
foreach (var arg in args) {
if (arg.Equals(CommandLineArgs.AllowMulti, StringComparison.CurrentCultureIgnoreCase))
allowMulti = true;
if (arg.Equals(CommandLineArgs.NoSplash, StringComparison.CurrentCultureIgnoreCase))
showSplashScreen = false;
}
// Try and initialize myself as the first instance. If I'm not and 'allowMulti' is false, exit with a return code of 1
if (!ApplicationBase.InitializeAsFirstInstance(ApplicationInfo.ProductName) && !allowMulti)
return 1;
if (showSplashScreen) {
var splashScreen = new SplashScreen("resources/images/splashscreen.png");
splashScreen.Show(true, false);
}
_ = new App();
return 0;
}
}
The advantage of this approach is it hands over execution even before the application itself is instantiated as well as before the splash screen is shown. In other words, it bails out at the earliest possible place.
Note: If you don't even want multi-support, then you can remove that argument check and test. This was just added for illustrative purposes
This is a simple solution, Open your startup file (View from where your application starts) in this case its MainWindow.xaml. Open your MainWindow.xaml.cs file. Go to the constructor and after intializecomponent() add this code:
Process Currentproc = Process.GetCurrentProcess();
Process[] procByName=Process.GetProcessesByName("notepad"); //Write the name of your exe file in inverted commas
if(procByName.Length>1)
{
MessageBox.Show("Application is already running");
App.Current.Shutdown();
}
Don't forget to add System.Diagnostics
Here is example that brings the old instance to foreground aswell:
public partial class App : Application
{
[DllImport("user32", CharSet = CharSet.Unicode)]
static extern IntPtr FindWindow(string cls, string win);
[DllImport("user32")]
static extern IntPtr SetForegroundWindow(IntPtr hWnd);
[DllImport("user32")]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32")]
static extern bool OpenIcon(IntPtr hWnd);
private static Mutex _mutex = null;
protected override void OnStartup(StartupEventArgs e)
{
const string appName = "LinkManager";
bool createdNew;
_mutex = new Mutex(true, appName, out createdNew);
if (!createdNew)
{
ActivateOtherWindow();
//app is already running! Exiting the application
Application.Current.Shutdown();
}
base.OnStartup(e);
}
private static void ActivateOtherWindow()
{
var other = FindWindow(null, "!YOUR MAIN WINDOW TITLE HERE!");
if (other != IntPtr.Zero)
{
SetForegroundWindow(other);
if (IsIconic(other))
OpenIcon(other);
}
}
}
But it will only work if your main window title do not change durig runtime.
Edit:
You can also use Startup
event in App.xaml
instead of overriding OnStartup
.
// App.xaml.cs
private void Application_Startup(object sender, StartupEventArgs e)
{
const string appName = "LinkManager";
bool createdNew;
_mutex = new Mutex(true, appName, out createdNew);
if (!createdNew)
{
ActivateOtherWindow();
//app is already running! Exiting the application
Application.Current.Shutdown();
}
}
// App.xaml
<Application x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp"
StartupUri="MainWindow.xaml" Startup="Application_Startup"> //<- startup event
Remember to not call base.OnStartup(e)
in this case!
I've used a simple TCP socket for this (in Java, 10 years ago).
My Solution for a .Net Core 3 Wpf Single Instance Application:
[STAThread]
public static void Main()
{
StartSingleInstanceApplication<CntApplication>();
}
public static void StartSingleInstanceApplication<T>()
where T : RichApplication
{
DebuggerOutput.GetInstance();
Assembly assembly = typeof(T).Assembly;
string mutexName = $"SingleInstanceApplication/{assembly.GetName().Name}/{assembly.GetType().GUID}";
Mutex mutex = new Mutex(true, mutexName, out bool mutexCreated);
if (!mutexCreated)
{
mutex = null;
var client = new NamedPipeClientStream(mutexName);
client.Connect();
using (StreamWriter writer = new StreamWriter(client))
writer.Write(string.Join("\t", Environment.GetCommandLineArgs()));
return;
}
else
{
T application = Activator.CreateInstance<T>();
application.Exit += (object sender, ExitEventArgs e) =>
{
mutex.ReleaseMutex();
mutex.Close();
mutex = null;
};
Task.Factory.StartNew(() =>
{
while (mutex != null)
{
using (var server = new NamedPipeServerStream(mutexName))
{
server.WaitForConnection();
using (StreamReader reader = new StreamReader(server))
{
string[] args = reader.ReadToEnd().Split("\t", StringSplitOptions.RemoveEmptyEntries).ToArray();
UIDispatcher.GetInstance().Invoke(() => application.ExecuteCommandLineArgs(args));
}
}
}
}, TaskCreationOptions.LongRunning);
typeof(T).GetMethod("InitializeComponent").Invoke(application, new object[] { });
application.Run();
}
}
1) It looks like a standard Dispose implementation to me. It is not really necessary (see point 6) but it does not do any harm. (Cleanup on closing it's a bit like cleaning the house before burning it down, IMHO, but opinions on the matter differs..)
Anyway, why not using "Dispose" as the name of the cleanup method, even if it does not get called directly? You could have called it "Cleanup", but remember you also write code for humans, and Dispose looks familiar and anyone on .NET understands what is it for. So, go for "Dispose".
2) I have always seen m_Mutex = new Mutex(false, mutexName);
I think it's more a convention that a technical advantage, however.
3) From MSDN:
If the message is successfully registered, the return value is a message identifier in the range 0xC000 through 0xFFFF.
So I would not worry. Usually, for this class of functions, UInt is not used for "it does not fit in Int, let's use UInt so we have something more" but to clarify a contract "function never returns a negative value".
4) I would avoid calling it if you will shutdown, same reason as #1
5) There are a couple of ways of doing it. The easiest way in Win32 is simply to have the second instance make the call to SetForegroundWindow (Look here: http://blogs.msdn.com/b/oldnewthing/archive/2009/02/20/9435239.aspx); however, I don't know if there is an equivalent WPF functionality or if you need to PInvoke it.
6)
For example... what happens if my application crashes between OnStartup and OnExit?
It's OK: when a process terminates, all handles owned by the process are released; the mutex is released as well.
In short, my recommendations:
For example, you can use your technique (trying to send/post a message to the window - if does not answer back it is stuck), plus MSK technique, to find and terminate the old process. Then start normally.