Last active
May 27, 2018 13:09
-
-
Save miteshsureja/4a577b200a4eda57ae27fff2e53f4430 to your computer and use it in GitHub Desktop.
How to run single instance of an application using Mutex?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Data; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Windows; | |
namespace WPFSingleInstanceApplication | |
{ | |
/// <summary> | |
/// Interaction logic for App.xaml | |
/// </summary> | |
public partial class App : Application | |
{ | |
private static Mutex mutex = null; | |
protected override void OnStartup(StartupEventArgs e) | |
{ | |
bool isFirstInstance; | |
mutex = new Mutex(true, "MyWPFApp", out isFirstInstance); | |
if (isFirstInstance) | |
{ | |
MessageBox.Show("Welcome, Running first instance of MyWPFApp", "Info"); | |
MainWindow window = new MainWindow(); | |
window.Show(); | |
} | |
else | |
{ | |
MessageBox.Show("One instance of MyWPFApp is already running.", "Info"); | |
Application.Current.Shutdown(); | |
} | |
base.OnStartup(e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace SingleInstanceApplication | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
bool isFirstInstance; | |
using (Mutex mutex = new Mutex(true, "MyApp", out isFirstInstance)) | |
{ | |
if (isFirstInstance) | |
{ | |
Console.WriteLine("Welcome, Running first instance of MyApp."); | |
Console.Read(); | |
} | |
else | |
{ | |
Console.WriteLine("One instance of MyApp is already running."); | |
Console.Read(); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace SingleInstanceApplication | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//get process count of running application | |
if (System.Diagnostics.Process.GetProcessesByName( | |
System.IO.Path.GetFileNameWithoutExtension( | |
System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1) | |
{ | |
Console.WriteLine("One instance is already running so closing this instance."); | |
Console.Read(); | |
System.Diagnostics.Process.GetCurrentProcess().Kill(); | |
} | |
else | |
{ | |
Console.WriteLine("Welcome, Running first instance."); | |
Console.Read(); | |
} | |
} | |
} | |
} |
Author
miteshsureja
commented
May 26, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment