Prevent multiple insances of an addin at once

A place for App developers to hang out / post

Prevent multiple insances of an addin at once

Post#1 » Thu May 10, 2018 2:42 pm

Preventing multiple instances of a Windows Media Center application from running at the same time from: https://web.archive.org/web/20070217001 ... b221d.aspx
By default, when a user clicks on an entry point in the Windows Media Center UI to launch 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.

In addition, Windows Media Center maintains a back stack of up to 8 applications so that the user can press the back button on the remote control and return to the previous experience. If a user clicks on an application entry point more than once, each instance of the application is also added to the Windows Media Center back stack up to the limit of 8 instances.

Because of this back stack behavior, it is possible to have up to 8 instances of the same Windows Media Center application running in separate ehExtHost processes on the user’s system. Having multiple instances of the same Windows Media Center application running on the system simultaneously can cause the following problems:
Performance – large applications can quickly consume a lot of system resources and slow down the overall system performance. This is particularly problematic for hosted XBAP applications because XBAPs are hosted by another executable named PresentationHost.exe in addition to ehExtHost.exe.
Shared resources – if a 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.
It is possible to code a Windows Media Center application to prevent multiple instances from being launched and running at the same time on the user’s system. Jossef Goldberg, a program manager on the Windows Presentation Foundation (WPF) team, has posted a sample application to demonstrate how to accomplish this. His example, which can be downloaded from this location, provides sample code for an XBAP application, but the concepts can be applied equally to Media Center Markup Language (MCML) applications or Windows Media Center Presentation Layer background applications.

This sample application includes 2 pieces:
A stub application entry point that is used to ensure single instancing
The “real” application entry point that contains the code and UI that the user sees within Windows Media Center
Both entry points are registered with Windows Media Center using the RegisterApplication API or the RegisterMceApp utility. However, only the stub entry point will appear within the Windows Media Center UI and allow the user to click on it to invoke it. The real entry point is registered with a hidden category so that the user cannot invoke it directly, which allows the stub entry point to manage invokation of the real entry point.
A mutex is created at the beginning of the Launch method in the stub entry point that is called by Windows Media Center when the user clicks on an entry point in the UI. If the mutex is acquired successfully, the stub entry point code calls the LaunchEntryPoint API 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 ReturnToApplication API to navigate to the instance of the real entry point that is already running in the Windows Media Center back stack.

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

public void Launch(AddInHost host)
{
// Create a named Mutex to check if 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);
}
}
You can also try out a runnable XBAP sample application that implements the above algorithm by downloading the ZIP file at this location and following the instructions in LaunchSingleInstance_ReadMe.doc inside of the ZIP file.
jachin99
 
Posts: 772
Joined: 24 February 2016

Return to Developers Corner



Who is online

Users browsing this forum: No registered users and 1 guest

cron