Preventing Multiple Instances of a Windows Media Center Application from Running Simultaneously

By default, when a user clicks an entry point in Windows Media Center to start a Windows Media Center application, a new instance of the Windows Media Center hosting process (ehExtHost.exe) is created and a new instance of the application is started within that hosting process. Windows Media Center also maintains a back stack of up to eight applications so that the user can press the Back button on the remote control and return to the previous experience. If a user selects an application entry point multiple times, each instance of the application (up to the limit of eight instances) is added to the Windows Media Center back stack.

Because of this back stack behavior, it is possible to have up to eight instances of the same Windows Media Center application running in separate ehExtHost processes on the user's system. However, multiple instances of the same Windows Media Center application running simultaneously can cause the following problems:

  • A decrease in performance. Large applications can quickly consume a lot of system resources and slow down the overall system performance.
  • Problems with shared resources. If a Windows Media Center application reads from and writes to shared data sources on the file system or in the registry, having multiple instances running at the same time can cause resource contention problems, race conditions, and other problems in the application code.

To avoid these problems, you can create a stub Windows Media Center application that launches the main Windows Media Center application. The stub should contain logic to prevent multiple instances from running simultaneously on the user's PC.

The two entry points for an application are registered with Windows Media Center using the ApplicationContext.RegisterApplication method or the RegisterMceApp.exe utility. However, only the stub entry point appears within the Windows Media Center UI and can be selected by the user to start the application. The real entry point is registered with a hidden category so that the user cannot invoke it directly. Therefore, the stub entry point manages the way the application is invoked.

A mutual exclusion (mutex) object is created at the beginning of the IAddInEntryPoint.Launch method in the stub entry point, which is called by Windows Media Center when the user selects the entry point. If the mutex is acquired successfully, the stub entry point code calls the MediaCenterEnvironment.LaunchEntryPoint method to create a new instance of the real entry point. If the mutex is already held and cannot be created, the stub entry point code calls the ApplicationContext.ReturnToApplication method to navigate to the instance of the real entry point that is already running in the Windows Media Center back stack.

The following example shows an implementation of the Launch method for a Windows Media Center stub entry point that can be used to ensure that only a single instance of an application is running at any given time. You must replace application_guid and entrypoint_guid with actual GUID values for your entry point.

public void Launch(AddInHost host)
{
    // Create a named mutex to check whether an instance of this application is already running.
    bool bMyMutexWasCreated;
    Mutex myMutex = new Mutex(true, "MyMediaCenterMutex", out bMyMutexWasCreated);
    if (!bMyMutexWasCreated)  
    {
      // If we get here, the application is already running; bring it to the foreground.
      host.ApplicationContext.ReturnToApplication();
    }
    else
    {
      // If we get here, the application is not running; launch it now.
      host.MediaCenterEnvironment.LaunchEntryPoint(new Guid("{application_guid}"), new Guid("{entrypoint_guid}"), null); 
    }
}

See Also