Quantcast
Channel: What is the correct way to create a single-instance WPF application? - Stack Overflow
Browsing latest articles
Browse All 40 View Live

Answer by Hanabi for What is the correct way to create a single-instance WPF...

Here is my entire App.xaml.cs, this code also brings the launched program instance to the foreground:public partial class App : Application{ private static Mutex _mutex = null;...

View Article



Answer by datchung for What is the correct way to create a single-instance...

Based Matt Davis' answer, wrapped into a class for convenience.public static class SingleAppInstanceChecker{ /// <summary> /// Arbitrary unique string /// </summary> private static Mutex...

View Article

Answer by std8590 for What is the correct way to create a single-instance WPF...

My favourite solution is from MVP Daniel Vaughan:Enforcing Single Instance Wpf ApplicationsIt use MemoryMappedFile to send command line arguments to the first instance:/// <summary>/// This class...

View Article

Answer by Alexandru Dicu for What is the correct way to create a...

Please check the proposed solution from here that uses a semaphore to determine if an existing instance is already running, works for a WPF application and can pass arguments from second instance to...

View Article

Answer by Vishnu Babu for What is the correct way to create a single-instance...

I use Mutex in my solution for preventing multiple instances.static Mutex mutex = null;//A string that is the name of the mutexstring mutexName = @"Global\test";//Prevent Multiple Instances of...

View Article


Answer by Deniz for What is the correct way to create a single-instance WPF...

I can't find a short solution here so I hope someone will like this:UPDATED 2018-09-20Put this code in your Program.cs:using System.Diagnostics;static void Main(){ Process thisProcess =...

View Article

Answer by A.J.Bauer for What is the correct way to create a single-instance...

A time saving solution for C# Winforms...Program.cs:using System;using System.Windows.Forms;// needs reference to Microsoft.VisualBasicusing Microsoft.VisualBasic.ApplicationServices; namespace...

View Article

Answer by Legends for What is the correct way to create a single-instance WPF...

[I have provided sample code for console and wpf applications below.]You only have to check the value of the createdNew variable (example below!), after you create the named Mutex instance.The boolean...

View Article


Answer by A.T. for What is the correct way to create a single-instance WPF...

Named-mutex-based approaches are not cross-platform because named mutexes are not global in Mono. Process-enumeration-based approaches don't have any synchronization and may result in incorrect...

View Article


Answer by Divins Mathew for What is the correct way to create a...

Simply using a StreamWriter, how about this?System.IO.File.StreamWriter OpenFlag = null; //globallyandtry{ OpenFlag = new StreamWriter(Path.GetTempPath() +"OpenedIfRunning");}catch...

View Article

Answer by newbieguy for What is the correct way to create a single-instance...

Not using Mutex though, simple answer:System.Diagnostics; ...string thisprocessname = Process.GetCurrentProcess().ProcessName;if (Process.GetProcesses().Count(p => p.ProcessName == thisprocessname)...

View Article

Answer by Pete for What is the correct way to create a single-instance WPF...

I like a solution to allow multiple Instances, if the exe is called from an other path. I modified CharithJ solution Method 1: static class Program { [DllImport("user32.dll")] private static extern...

View Article

Answer by kakopappa for What is the correct way to create a single-instance...

Here is my 2 cents static class Program { [STAThread] static void Main() { bool createdNew; using (new Mutex(true, "MyApp", out createdNew)) { if (createdNew) { Application.EnableVisualStyles();...

View Article


Answer by pStan for What is the correct way to create a single-instance WPF...

This is how I ended up taking care of this issue. Note that debug code is still in there for testing. This code is within the OnStartup in the App.xaml.cs file. (WPF) // Process already running ? if...

View Article

Answer by Siarhei Kuchuk for What is the correct way to create a...

Here's the same thing implemented via Event.public enum ApplicationSingleInstanceMode{ CurrentUserSession, AllSessionsOfCurrentUser, Pc}public class ApplicationSingleInstancePerUser: IDisposable{...

View Article


Answer by Code Scratcher for What is the correct way to create a...

Here is a solution:Protected Overrides Sub OnStartup(e As StartupEventArgs) Const appName As String = "TestApp" Dim createdNew As Boolean _mutex = New Mutex(True, appName, createdNew) If Not createdNew...

View Article

Answer by Martin Bech for What is the correct way to create a single-instance...

I added a sendMessage Method to the NativeMethods Class.Apparently the postmessage method dosent work, if the application is not show in the taskbar, however using the sendmessage method solves...

View Article


Answer by Antoine Diekmann for What is the correct way to create a...

You can also use the CodeFluent Runtime which is free set of tools. It provides a SingleInstance class to implement a single instance application.

View Article

Answer by Eric Ouellet for What is the correct way to create a...

Update 2017-01-25. After trying few things, I decided to go with VisualBasic.dll it is easier and works better (at least for me). I let my previous answer just as reference...Just as reference, this is...

View Article

Answer by Jason Lim for What is the correct way to create a single-instance...

Here's a lightweight solution I use which allows the application to bring an already existing window to the foreground without resorting to custom windows messages or blindly searching process...

View Article

Answer by Cornel Marian for What is the correct way to create a...

Use mutex solution:using System;using System.Windows.Forms;using System.Threading;namespace OneAndOnlyOne{static class Program{ static String _mutexID = " // generate guid" /// <summary> /// The...

View Article


Answer by Simon Mourier for What is the correct way to create a...

The code C# .NET Single Instance Application that is the reference for the marked answer is a great start.However, I found it doesn't handle very well the cases when the instance that already exist has...

View Article


Answer by Dan for What is the correct way to create a single-instance WPF...

The following code is my WCF named pipes solution to register a single-instance application. It's nice because it also raises an event when another instance attempts to start, and receives the command...

View Article

Answer by Tommaso Belluzzo for What is the correct way to create a...

Normally, this is the code I use for single-instance Windows Forms applications:[STAThread]public static void Main(){ String assemblyName = Assembly.GetExecutingAssembly().GetName().Name; using (Mutex...

View Article

Answer by carlito for What is the correct way to create a single-instance WPF...

Look at the folllowing code. It is a great and simple solution to prevent multiple instances of a WPF application.private void Application_Startup(object sender, StartupEventArgs e){ Process thisProc =...

View Article


Answer by CharithJ for What is the correct way to create a single-instance...

This code should go to the main method. Look at here for more information about the main method in WPF.[DllImport("user32.dll")]private static extern Boolean ShowWindow(IntPtr hWnd, Int32...

View Article

Answer by Joel Barsotti for What is the correct way to create a...

It looks like there is a really good way to handle this:WPF Single Instance ApplicationThis provides a class you can add that manages all the mutex and messaging cruff to simplify the your...

View Article

Answer by Nathan Moinvaziri for What is the correct way to create a...

Here is an example that allows you to have a single instance of an application. When any new instances load, they pass their arguments to the main instance that is running.public partial class App :...

View Article

Answer by Mikhail Semenov for What is the correct way to create a...

I found the simpler solution, similar to Dale Ragan's, but slightly modified. It does practically everything you need and based on the standard Microsoft WindowsFormsApplicationBase class.Firstly, you...

View Article



Answer by Simon_Weaver for What is the correct way to create a...

MSDN actually has a sample application for both C# and VB to do exactly this: http://msdn.microsoft.com/en-us/library/ms771662(v=VS.90).aspxThe most common and reliable technique for developing...

View Article

Answer by Sergey Aldoukhov for What is the correct way to create a...

Here is what I use. It combined process enumeration to perform switching and mutex to safeguard from "active clickers":public partial class App{ [DllImport("user32")] private static extern int...

View Article

Answer by huseyint for What is the correct way to create a single-instance...

A new one that uses Mutex and IPC stuff, and also passes any command line arguments to the running instance, is WPF Single Instance Application.

View Article

Answer by Peter for What is the correct way to create a single-instance WPF...

So many answers to such a seemingly simple question. Just to shake things up a little bit here is my solution to this problem.Creating a Mutex can be troublesome because the JIT-er only sees you using...

View Article


Answer by Oliver Friedrich for What is the correct way to create a...

Well, I have a disposable Class for this that works easily for most use cases:Use it like this:static void Main(){ using (SingleInstanceMutex sim = new SingleInstanceMutex()) { if...

View Article

Answer by Bruce for What is the correct way to create a single-instance WPF...

Just some thoughts:There are cases when requiring that only one instance of an application is not "lame" as some would have you believe. Database apps, etc. are an order of magnitude more difficult if...

View Article

Answer by Matt Davis for What is the correct way to create a single-instance...

Here is a very good article regarding the Mutex solution. The approach described by the article is advantageous for two reasons.First, it does not require a dependency on the Microsoft.VisualBasic...

View Article


Answer by Matt Davison for What is the correct way to create a...

You should never use a named mutex to implement a single-instance application (or at least not for production code). Malicious code can easily DoS (Denial of Service) your ass...

View Article


Answer by Dale Ragan for What is the correct way to create a single-instance...

You could use the Mutex class, but you will soon find out that you will need to implement the code to pass the arguments and such yourself. Well, I learned a trick when programming in WinForms when I...

View Article

Answer by jason saldo for What is the correct way to create a single-instance...

From here.A common use for a cross-process Mutex is to ensure that only instance of a program can run at a time. Here's how it's done:class OneAtATimePlease { // Use a name unique to the application...

View Article

What is the correct way to create a single-instance WPF application?

Using C# and WPF under .NET (rather than Windows Forms or console), what is the correct way to create an application that can only be run as a single instance?I know it has something to do with some...

View Article
Browsing latest articles
Browse All 40 View Live


Latest Images