Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Single instance Windows application helper in C#
/*
* Copyright (c) 2021 Pavel @ keenthinker.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Threading;
using System.Windows.Forms;
namespace Application
{
/// <summary>
/// Helper class that encapsulates the execution of an action only once at the first call.
/// Subsequent calls of the same action will be ignored, as long as the first action call has not finished.
/// </summary>
public static class SingleInstance
{
/// <summary>
/// Execute the first action once or execute the alternative action when the first is not yet finished.
/// </summary>
/// <param name="uniqueApplicationDescription">Unique name of the mutex. Name should be choosen carefully not to collide with other mutexes.</param>
/// <param name="applicationTitle">Application title shown in the default message box.</param>
/// <param name="oneInstanceAction">First action that is executed only once.</param>
/// <param name="instanceNotUniqueAction">Action that is executed every time if the first action is still running.</param>
public static void Start(string uniqueApplicationDescription, string applicationTitle, Action oneInstanceAction, Action instanceNotUniqueAction = null)
{
bool isFirstInstance = true;
Mutex mutex = null;
try
{
// if there is no custom second action, set a default one.
if (instanceNotUniqueAction == null)
{
instanceNotUniqueAction = () => MessageBox.Show("Application is already running!", applicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//
mutex = new Mutex(true, uniqueApplicationDescription, out isFirstInstance);
if (isFirstInstance)
{
oneInstanceAction();
}
else
{
instanceNotUniqueAction();
}
}
finally
{
if (isFirstInstance)
{
if (mutex != null)
{
mutex.ReleaseMutex();
}
}
}
}
}
}
/*
/// <summary>
/// Example usage to create a single instance Windows application.
/// The main entry point for the application.
/// </summary>
static void Main()
{
SingleInstance.Start("Unique_String_for_example_a_GUID", "Application Title", () =>
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new ApplicationForm());
});
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment