Single Instance Detection Sample Home
This page is specific to:.NET Framework Version:3.03.54.0
Windows Presentation Foundation Samples
Single Instance Detection Sample

This sample demonstrates how to implement single instance detection for standalone applications.

This sample demonstrates a specific feature of the Windows Presentation Foundation and, consequently, does not follow application development best practices. For comprehensive coverage of Windows Presentation Foundation (WPF) and Microsoft .NET Framework application development best practices, refer to the following as appropriate:

Accessibility - Accessibility Best Practices

Localization - WPF Globalization and Localization Overview

Performance - Optimizing WPF Application Performance

Security - Windows Presentation Foundation Security

Download sample

Building the Sample

  • Install the Windows Software Development Kit (SDK) and open its build environment command window. On the Start menu, point to All Programs, Microsoft Windows SDK, and then click CMD Shell.

  • Download the sample, usually from the software development kit (SDK) documentation, to your hard disk drive.

  • To build the sample from the build environment command window, go to the source directory of the sample. At the command prompt, type MSBUILD.

  • To build the sample in Microsoft Visual Studio, load the sample solution or project file and then press CTRL+SHIFT+B.

Running the Sample

  • To run the compiled sample from the build environment command window, execute the .exe file in the Bin\Debug or Bin\Release folder contained under the sample source code folder.

  • To run the compiled sample with debugging in Visual Studio, press F5.

Remarks

The most common and reliable technique for developing single-instance detection is to use the Microsoft .NET Framework remoting infrastructure (System.Remoting). The Microsoft .NET Framework (version 2.0) includes a type, WindowsFormsApplicationBase, which encapsulates the required remoting functionality. To incorporate this type into a WPF application, a type needs to derive from it, and be used as a shim between the application static entry point method, Main, and the WPF application's Application type. The shim detects when an application is first launched, and when subsequent launches are attempted, and yields control the WPF Application type to determine how to process the launches.

NoteNote:

WindowsFormsApplicationBase is capable of starting its own message processing, although it is never started using the technique demonstrated in this sample. Instead, the WPF message pump processes as per normal.

See Also

Reference

Community Content

WPF application Single Instance in one line of code
Added by:Il-ya Tret-yakov

WPF application Single Instance in one line of code

By EventWaitHandle

Source code:

http://www.codeplex.com/WpfSingleInstance/Release/ProjectReleases.aspx?ReleaseId=20316

Tiny omission
Added by:TimHaughton
Most apps will use an App.xaml too, in this case, SingleInstanceManager has a tiny omission that will break most WPF applications - a call to InitializeComponent, method should read:

protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
{
// First time app is launched
app = new App();
app.InitializeComponent();
app.Run();
return false;
}

Without it, the app won't resolve resources.
Another possible solution
Added by:BladeWise
Another possible solution, without the need of a manager or the usage of WindowsFormsApplicationBase can be found here
http://www.codeplex.com/wpfinstanceawareapp

This solution defines a class inheriting from System.Windows.Application and uses a named Mutex to deal with application instance identification, and WCF named pipes to account for inter-process communication.
© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View