Click here to Skip to main content
15,499,046 members
Articles / Desktop Programming / WPF
Technical Blog
Posted 28 May 2010


60 bookmarked

WPF Single Instance Application

Rate me:
Please Sign up or sign in to vote.
4.93/5 (48 votes)
28 May 2010Ms-PL2 min read
How to enforce that your WPF application has only one instance

The Problem

The question this post solves is how to enforce that your WPF application has only one instance?

Solution Source

The solution is based on code found in some WPF reference applications which Microsoft will soon (?) release. I didn't write it, but I used it several times and it’s the best solution I've found to date, so I'd hate to see it unpublished.

Solution Advantages

So, what are the advantages of this solution? After all, it’s not the first time someone posts a solution for this problem.

Well, the most important advantage is that it works.
No glitches. No special cases. No inherent race conditions.
It simply works.

Second, it’s easily used. In the next section, I'll show you exactly how to use it.

Third, there are no constraints on your WPF application. Specifically, your main application / window class doesn't have to inherit some base class for this to work.

And at last, you don't need to be dependent on any VB DLL.
I say this because one of the popular solutions for this problem requires you to add a reference to a (WinForms related!) Visual Basic DLL, which may feel strange for some people.

As a bonus, the solution provides to the first instance the parameters of the second instance when run. This is very useful if you want to integrate you application with the Windows 7 taskbar.

Solution Details

So, let’s see how to make your WPF application having just one instance.

Step 1: Add the file SingleInstance.cs to your project.

Step 2: Add a reference to your project: System.Runtime.Remoting.

Step 3: Have your application class implement ISingleInstanceApp (defined in SingleInstance.cs).

The only method in this interface is:

bool SignalExternalCommandLineArgs(IList<string> args)

This method is called when a second instance of your application tries to run. It has an args parameter which is the same as the command line arguments passed to the second instance.

Step 4: Define your own Main function that uses the single instance class.

Your App class should now be similar to this:

/// <span class="code-SummaryComment"><summary>
</span>/// Interaction logic for App.xaml
/// <span class="code-SummaryComment"></summary>
</span>public partial class App : Application, ISingleInstanceApp
    private const string Unique = "My_Unique_Application_String";
    public static void Main()
        if (SingleInstance<App>.InitializeAsFirstInstance(Unique))
            var application = new App();
            // Allow single instance code to perform cleanup operations
    #region ISingleInstanceApp Members
    public bool SignalExternalCommandLineArgs(IList<string> args)
        // handle command line arguments of second instance
        // ...
        return true;

Step 5: Set new main entry point.

Select Project Properties –> Application and set “Startup object” to your App class name instead of “(Not Set)”.

Step 6: Cancel the default WPF main function.

Right click on App.xaml, Properties, set Build Action to "Page" instead of "Application Definition".

Solution Inner Works

I can summarize it as: Mutex and Remoting, done right.
If you want to know more details, just have a look at the code.

You can find a WPF sample application here.

That’s it for now,
Arik Poznanski.

kick it on Shout it
This article was originally posted at


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Written By
Software Developer (Senior) Verint
Israel Israel
Arik Poznanski is a senior software developer at Verint. He completed two B.Sc. degrees in Mathematics & Computer Science, summa cum laude, from the Technion in Israel.

Arik has extensive knowledge and experience in many Microsoft technologies, including .NET with C#, WPF, Silverlight, WinForms, Interop, COM/ATL programming, C++ Win32 programming and reverse engineering (assembly, IL).

Comments and Discussions

PraiseMy vote 5 Pin
Ammar Shaukat9-Apr-20 0:45
professionalAmmar Shaukat9-Apr-20 0:45 
Questionthx Pin
Tomáš Bouda12-Oct-18 3:42
MemberTomáš Bouda12-Oct-18 3:42 
QuestionCannot download your code Pin
Member 820516928-Aug-15 3:22
MemberMember 820516928-Aug-15 3:22 
QuestionApp.xaml conflict? Pin
Keith O. Williams5-Jul-15 16:41
professionalKeith O. Williams5-Jul-15 16:41 
QuestionMethodSignalExternalCommandLineArgs Not call Pin
Levellak29-Dec-14 9:13
MemberLevellak29-Dec-14 9:13 
QuestionIssue with .net framework 4.5.1 Pin
ysri11-Feb-14 4:20
Memberysri11-Feb-14 4:20 
AnswerRe: Issue with .net framework 4.5.1 Pin
ysri4-Mar-14 16:28
Memberysri4-Mar-14 16:28 
GeneralRe: Issue with .net framework 4.5.1 Pin
Levellak29-Dec-14 4:16
MemberLevellak29-Dec-14 4:16 
GeneralMy vote of 5 Pin
Petar Tseperski22-Jan-14 1:03
MemberPetar Tseperski22-Jan-14 1:03 
QuestionHow does it compare to using EventWaitHandle? Pin
yannlh20-Jan-14 4:49
Memberyannlh20-Jan-14 4:49 
Bugcatch { } Pin
CoperNick13-Oct-13 23:08
MemberCoperNick13-Oct-13 23:08 
QuestionHow to bring the window into view? Pin
Christoph19729-Mar-13 8:36
MemberChristoph19729-Mar-13 8:36 
AnswerRe: How to bring the window into view? Pin
Member 787310314-Mar-13 23:50
MemberMember 787310314-Mar-13 23:50 
AnswerThis saved my day Pin
Umpfz4-Nov-12 5:23
MemberUmpfz4-Nov-12 5:23 
GeneralMy vote of 5 Pin
Anand Murali M S19-Sep-12 20:48
MemberAnand Murali M S19-Sep-12 20:48 
QuestionGreat article, but - how do we prevent the loss of inheritance of the App.xaml resources? Pin
JamesHurst12-Aug-12 23:01
MemberJamesHurst12-Aug-12 23:01 
AnswerRe: Great article, but - how do we prevent the loss of inheritance of the App.xaml resources? Pin
N Jones24-Sep-12 4:33
MemberN Jones24-Sep-12 4:33 
GeneralRe: Great article, but - how do we prevent the loss of inheritance of the App.xaml resources? Pin
JamesHurst24-Jan-13 9:29
MemberJamesHurst24-Jan-13 9:29 
Where does Microsoft.Shell come from?
James Hurst
"The greatness of a nation and its moral progress can be judged by the way its animals are treated."

Mahatma Gandhi

GeneralMy vote of 5 Pin
Martin Mikula VSBTU17-Jul-12 5:15
MemberMartin Mikula VSBTU17-Jul-12 5:15 
GeneralMy vote of 5 Pin
Jennie MG22-Jun-12 9:47
MemberJennie MG22-Jun-12 9:47 
GeneralMy vote of 5 Pin
Member 867463424-Mar-12 11:36
MemberMember 867463424-Mar-12 11:36 
GeneralMy vote of 4 Pin
NinjaCross17-Feb-12 1:46
MemberNinjaCross17-Feb-12 1:46 
QuestionHelp needed Pin
Member 858049131-Jan-12 0:03
MemberMember 858049131-Jan-12 0:03 
QuestionMy vote of 5, however doesn't work on Terminal Services Pin
p.moretti29-Jan-12 23:12
Memberp.moretti29-Jan-12 23:12 
GeneralMy vote of 5 Pin
EbrahimAsadi8-Jul-11 22:47
MemberEbrahimAsadi8-Jul-11 22:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.