The Windows Media Center Platform Team Blog RSS 2.0
 Sunday, September 17, 2006

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:

  1. A stub application entry point that is used to ensure single instancing
  2. 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.

Aaron

 

Categories: Sample | Comments [2] | # | Posted on Sunday, September 17, 2006 7:16:13 PM (GMT Standard Time, UTC+00:00)   
 Saturday, September 16, 2006

Update: I added a few stations and changed the registration to use the /allusers switch (so this will show up on Media Center Extender) so pay attention to the setup instructions below.

Coding Friday resulted in a little Windows Media Center Presentation Layer Web Application called Veronicas Radio, named after a Program Manager on the Windows Media Center team who came to me one day and said 'hey, it would be cool if I could have all of my favorite streaming radio stations in a customized UI, just for me'. At least I remember the conversation going something like that. Anywho...

I took the helix we did for the Q app (see http://play.mediacentersandbox.com/mcml/rc1/helix.mcml for the codeless version) and added a call to PlayMedia to create a streaming radio URI. After applying a Gaussian blur to one of the sample pictures which ship with Windows to represent a background (nice pink) and station images (culled / created from their respective websites) we now have something that looks like this in Windows Media Center...

You can install this web app to your Diamond RC1 machine as follows:

  1. Download http://play.mediacentersandbox.com/mcml/rc1/setup.veronicasradio.zip
  2. Unzip the contents to your local machine.
  3. Open a command prompt with Administrator priviledges.
  4. Run setup.veronicasradio.cmd.
  5. Launch Windows Media Center and Select ‘Veronicas Radio’ in Program Library.

We think it would be pretty cool to see what other mods or hacks folks could do with the helix. Hit this URL with IE > View Source: http://play.mediacentersandbox.com/mcml/rc1/veronicasradio.mcml, modify to your liking, post to your own web server, post an installer (everything you need is in the install download for Veronicas Radio) and let's see what neat mashups you can create.

Charlie

Categories: Sample | Comments [1] | # | Posted on Friday, September 15, 2006 11:26:48 PM (GMT Standard Time, UTC+00:00)   
 Thursday, September 14, 2006

I have been working on some Windows Media Center application debugging and deployment scenarios recently so that we can enhance some of the documentation in the Windows Media Center SDK for Windows Vista.  I want to document one of the interesting configurations here in case it is useful in your Windows Media Center application development scenarios.

Before I get started, I want to note that the steps listed below work equally well if you are developing in Visual C# 2005 Express Edition or Visual Studio 2005 Standard or higher.  For simplicity's sake, I will refer to both by the single name "Visual Studio" in the instructions below.

Step 1: Getting started

I started by installing Windows Vista RC1 (Ultimate Edition), Visual C# 2005 Express Edition and the Windows Media Center SDK for Windows Vista RC1.  Then, I created a Visual C# Windows Media Center Presentation Layer (WMCPL) application in Visual C# Express.

By default, the WMCPL application is configured to launch the standalone version of McmlPad and navigate to the file default.mcml that is a part of the project.  The steps below allow you to configure the WMCPL project so that it will register the application with Windows Media Center and automatically launch the Windows Media Center shell and navigate to the application when pressing F5 to build and debug in Visual Studio.  The steps require you to make some modifications directly to the .csproj file, and then make some modifications to the project settings in the Visual Studio IDE.

Step 2: Modify settings in the .csproj file

Open the .csproj file for your WMCPL application project (located at c:\Users\<username>\Documents\Visual Studio 2005\Projects\<solution name>\<project name> by default) in a text editor such as notepad and make the following changes:

  1. Override the pre-build target so that Visual Studio will ignore pre-build event errors using the technique described in this blog post.  You can do this by adding the following section to the bottom of the  .csproj file just before the closing </Project> tag:

      <Target Name="PreBuildEvent" Condition="'$(PreBuildEvent)'!=''" DependsOnTargets="$(PreBuildEventDependsOn)">
        <Exec WorkingDirectory="$(OutDir)" Command="$(PreBuildEvent)" IgnoreExitCode="true" />
      </Target>
  2. Change the <StartProgram> to be $(windir)\eHome\ehshell.exe (replacing the default value of $(windir)\eHome\McmlPad.exe)
  3. Change the <StartArguments> to be /entrypoint:{<app_guid>}\{<entrypoint_guid>} /addinfallbackpath:"$(FullyQualifiedOutputPath)" (where <app_guid> and <entrypoint_guid> are the GUIDs listed in the App.xml file that is generated when you create a new Windows Media Center Presentation Layer Application)
  4. Save the changes and close the .csproj file

Step 3: Update project settings in the Visual Studio IDE

After modifying and saving the WMCPL application .csproj file using the steps listed above, open it in Visual Studio and perform the following steps:

  1. Right-click on the project in Solution Explorer and choose Properties
  2. Click on the Build Events tab in the Property Pages
  3. Add the following text to the Pre-build event command line text box: %windir%\eHome\RegisterMceApp.exe /u App.xml
  4. Add the following text to the end of the Post-build event command line text box (make sure to leave the command line there that appears by default to run mcmlverifier.exe to validate the MCML syntax in your project): %windir%\eHome\RegisterMceApp.exe App.xml
  5. Click on the File menu and choose Save All to save the updated project settings

Once you have completed all of the above steps, you can press F5 in the Visual Studio IDE to build and run the WMCPL application, and it will launch Windows Media Center and navigate directly to the application by using the /entrypoint command line switch that is available for ehShell.exe.

I have found these steps useful when I want to rapidly preview a WMCPL application hosted within Windows Media Center because I do not need to manually register the application and add the application assembly to the global assembly cache (GAC) each time I rebuild it.  Note that you will want to eventually add a strong-name key to your assembly, create an installer for your application (using a technique such as this) and install the assembly to the GAC for real-world deployment scenarios.

Unfortunately, you cannot use Visual Studio to debug the application code if you directly launch it in Windows Media Center using the above steps.  Visual Studio attaches to the process that it launches (which in this case is the Windows Media Center shell application - ehShell.exe).  Each application hosted in Windows Media Center is then launched in a new instance of the Windows Media Center hosting application - ehExtHost.exe.  That ehExtHost process is the one that you need to attach to in order to debug the application code.  You can, however, use steps like the ones documented in this previous blog post to attach and debug application code within ehExtHost.exe.

Hopefully you will find this technique useful in some of your Windows Media Center application development scenarios.

Aaron

 

Categories: Sample | Comments [1] | # | Posted on Thursday, September 14, 2006 4:31:00 AM (GMT Standard Time, UTC+00:00)   
Blogroll
About

Disclaimer
All information available via this site is provided 'as is' with no warranties and confers no rights.

© Copyright 2008 Microsoft Corporation.

Sign In
All Content © 2008, Microsoft Corporation.