37

How do I allow only one instance of a WPF application to run?

Thanks.

John Batdorf
  • 2,502
  • 8
  • 35
  • 43
  • 3
    http://stackoverflow.com/questions/19147/what-is-the-correct-way-to-create-a-single-instance-application – Donnelle Jan 05 '09 at 22:41

5 Answers5

30

Old link no longer available :/: http://blogs.microsoft.co.il/blogs/arik/archive/2010/05/28/wpf-single-instance-application.aspx

Suggest having at update posted by @sergio-basurco

Doesn't require VB.DLL as some other examples advise. Has WPF sample code. Passes any cmd line args to initial instance.

sobelito
  • 1,525
  • 17
  • 13
  • 2
    I really like this method, it allows you to do things like activate a minimized window if the user tries to run the program again. – Andy Dec 13 '12 at 14:50
  • This is under a non-commercial Microsoft blog [license](https://www.microsoft.com/en-us/legal/intellectualproperty/copyright/default.aspx). The resource has been released in msdn with a different license. See my answer on this thread. – Sergio Basurco Mar 01 '17 at 11:37
  • I'm afraid the link is dead now – Damian Ubowski Jan 14 '23 at 17:12
19

Try this: Single instance application. Ive used the second method and it works fine.

Artur Carvalho
  • 6,901
  • 10
  • 76
  • 105
  • 1
    the second method is the best i've found. just get over the fact it uses Microsoft.VisualBasic DLL and it does everything else you need - including proper use of .NET remoting (no sockets or mutexes to create yourself). but best of all it makes it easy to pass parameters to the already running application, or simply bring it to the front when you try to reopen it – Simon_Weaver Sep 27 '10 at 22:25
  • Broken link - can you please update – rayzinnz Dec 22 '21 at 20:51
  • 1
    @rayzinnz should be good – Artur Carvalho Dec 23 '21 at 08:53
2

Check out this solution: Allowing only one instance of a WPF application to execute

This not only enforces one instance of an application, but it also gives your current application focus when an additional instance of an application is ran. My mutex solution to restricting one instance is actually different from the above link, but I liked the "focus" element to this solution.

jsirr13
  • 944
  • 2
  • 12
  • 38
2

I use this helper method and call it from the application.startup event

    Public Sub ForceSingleInstanceApplication()
        'Get a reference to the current process
        Dim MyProc As Process = Process.GetCurrentProcess

        'Check how many processes have the same name as the current process
        If (Process.GetProcessesByName(MyProc.ProcessName).Length > 1) Then
            'If there is more than one, it is already running
            MsgBox("Application is already running", MsgBoxStyle.Critical, My.Application.Info.Title) 'Reflection.Assembly.GetCallingAssembly().GetName().Name)
            ' Terminate this process and give the operating system the specified exit code.
            Environment.Exit(-2)
            Exit Sub
        End If
    End Sub
VoteCoffee
  • 4,692
  • 1
  • 41
  • 44
1

User sobelito linked this post, which has the following update. What it says is that for an updated resource you should use Windows 7 Taskbar Single Instance, which if you look into the source will allow you to do what you need.

You can use the SingleInstance c# project. It also contains samples for both WinForms and WPF.

Note that it's also released under the Apache 2.0 license, unlike Arik's Poznanski post in the Microsoft Blog, which is (IANAL, AFAIK) not commercially available.

Sergio Basurco
  • 3,488
  • 2
  • 22
  • 40