Application Instancing

While we've just been talking about the lifetime of an application, things get a bit more interesting when you take into account that multiple instances of a single application can be running at any one time simply because the user can double-click on the same EXE multiple times. In fact, the default behavior in Windows and WPF does nothing to hamper or support multiple instances of the same application. For example, if we double-click on the AppWindowsSample.exe from Figure 2-2 more than once, we get more than one instance, as Figure 2-3 shows.

Figure 2-3. Multiple instance applications

In Figure 2-3, we've got several top-level windows, some associated with each of three instances of the application. Sometimes more than one instance of a single application is a good thing. However, sometimes it just confuses users. For example, in Figure 2-3, even though we've got a Window menu, only the windows associated with each instance of the application are shown, which can be confusing as heck to the poor user faced with such an application.

Single instance applications

If you'd like your application to be single instance, it's easy to detect an existing instance and shut down any subsequent instances (see Example 2-5).

Example 2-5. Very simple existing instance detection public partial class App : System.Windows.Application { Mutex mutex;

protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e);

// Check for existing instance string mutexName = "MyCompanyName.MyAppName";

bool createdNew;

mutex = new Mutex(true, mutexName, out createdNew);

// If there is an existing instance, shut down this one if( !createdNew ) { Shutdown(); }

In Example 2-5, the key is to access a Windows mutex with a session-wide unique name so that we can tell whether it was already created by an initial instance or whether we're the initial instance. The mutex name we're using is one we pick to be sufficiently unique for our needs. Once the mutex has been created, it'll live for the life of the WPF Application object itself, which will live for the life of the process, so if we're not the first one to create the mutex, we shut down our application, causing our process to exit.

However, it's at this point that we realize that single instance detection isn't the only feature we want; we also want the following:

• Passing command-line arguments to the initial instance (e.g., in case a subsequent instance was passed a filename that the user would like opened)

• Activating the main window of the initial instance

• Dealing properly with multiple users logging into a single computer (even the same user logged in multiple times), giving each login an instance of the application

These are services that are not trivial to implement and we'd really love it if .NET provided this functionality for us. The good news is that it does. The bad news is that it's provided only as part of the .NET 2.0 Visual Basic support for Windows Forms.

If you'd like to take advantage of robust single instance management, you have to load the Microsoft.VisualBasic assembly, and you have an interesting integration challenge ahead of you, as both Visual Basic's support for single instance management and WPF want to be "the" application. However, it is possible and you get to leverage Other People's Code (OPC), of which I'm a big fan, especially when the "other people" are a multibillion-dollar corporation with a record of framework maintenance and upgrades.* For an example of how to integrate single instance detection from Visual Basic into WPF, check out the "Single Instance Detection" sample in the Windows Platform SDK.t

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment