Initializing, Launching, and Uninitializing the Application

Initializing the Application

All Windows Media Center managed-code applications must implement two interfaces: IAddInModule and IAddInEntryPoint. These interfaces expose methods that are called by Windows Media Center.

  • IAddInModule exposes the Initialize and Uninitialize methods.
  • Initialize is the first method that Windows Media Center calls when it loads the application, giving the application an opportunity to initialize internal variables and obtain any system resources that it needs.

Launching the Application

After calling IAddInModule.Initialize, Windows Media Center calls an application's IAddInEntryPoint.Launch method, passing an instance of the AddInHost object to the application. This object lets the application access objects in the Microsoft.MediaCenter namespace to retrieve information about Windows Media Center and to control certain aspects of the Windows Media Center experience.

The AddInHost object is guaranteed to be valid only until the Launch method returns. Therefore an application must make all calls to the Windows Media Center APIs within the context of the Launch method. Attempting to call the AddInHost object after the Launch method returns can result in a fatal error. Multiple threads spawned by the Launch method must be terminated before the Launch method returns.

After calling the Launch method, if you do not use the host object, the object could be released because .NET Remoting releases objects every five minutes if they are not used. To avoid this, use the host object or use the objects within five minutes to prevent them from being released.

Terminating the Application

Before terminating an application, Windows Media Center calls the application's IAddInModule.Uninitialize method and then briefly waits for the application to save its state, terminate any threads it created, and free any other system resources that it may have allocated. If the application's Uninitialize method does not return quickly enough, Windows Media Center unloads the application's application domain, terminating the application.

You should assume that your application can be interrupted at any time (for instance, if the user closes Windows Media Center). Design the application to perform lengthy operations in small steps, saving its state after each step if necessary. Using this approach, the application can continue if it was previously interrupted.

If your application needs a more deterministic environment, you should implement the application as an executable program that runs outside of Windows Media Center and use a helper application to manipulate the Windows Media Center aspects of the application.

Sample Code

For an example, create an application using one of the Windows Media Center project templates. The Launch.cs file contains the code for initializing, launching, and terminating the application.

See Also