Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Reducing CPU Usage

Reducing CPU Usage

When a Microsoft Surface application is deactivated (that is, it loses focus), its process thread continues running in the background. To maximize the operating efficiency of a device made for Surface, an application should stop capturing raw images and avoid audio playback, video processing, or other CPU-intensive processing that might use resources that should be available to the current foreground application. The Microsoft.Surface.ApplicationServices class provides event handlers that can notify an application when it is being deactivated. With this event information, you can make sure that an application knows when to avoid using CPU intensive code segments.

Application Events

The Surface project templates for Microsoft Visual Studio 2010 (and Visual Studio 2010 Express) register event handler methods for the ApplicationServices events:

  • WindowInteractive

  • WindowNoninteractive

  • WindowUnavailable

You can add logic to the event handler methods, or in the rest of your code, to economize CPU usage.

For example, in a Microsoft XNA Game Studio 4.0 project, the following code in the App1.cs file uses the WindowAvailability property to prevent a deactivated process from gaining access to the Update method.

protected override void Update(GameTime gameTime)
    if (ApplicationServices.WindowAvailability != WindowAvailability.Unavailable)
        if (ApplicationServices.WindowAvailability == WindowAvailability.Interactive)
            // TODO: Process touches, 
            // use the following code to get the state of all current touch points.
            // ReadOnlyTouchPointCollection touches = touchTarget.GetState();

        // TODO: Add your update logic here


Using the Event Handlers in an XNA Application

The following example demonstrates how to enable and disable raw images during application events.

This example comes from the SDK sample RawImage Visualizer, which is available in the %ProgramFiles%\Microsoft SDKs\Surface\v2.0\Samples folder after you install the Surface SDK. The RawImage Visualizer sample obtains an image from the Surface vision system, capturing the appearance of objects that touch the application window, and displaying the inverted image in the other half of the application window. The following code is from the App1.cs file.

private TouchTarget touchTarget;

private void OnWindowInteractive(object sender, EventArgs e)
    // Turn raw image back on again

private void OnWindowUnavailable(object sender, EventArgs e)
    // If the app isn't active, there's no need to keep the raw image enabled

Using the Event Handlers in a WPF Application

The following example demonstrates how to mute the audio of a MediaElement object when an application enters a non-interactive state.

This example comes from the SDK sample, Photo Paint, which is available in the %ProgramFiles%\Microsoft SDKs\Surface\v2.0\Samples folder after you install the Surface SDK. The Photo Paint sample displays an image and a video, each overlaid with a SurfaceInkCanvas in which the user can draw strokes. When the application window enters a non-interactive state, the video sound is muted in the WindowNoninteractive event in the Window1.xaml.cs file.

private void OnWindowNoninteractive(object sender, EventArgs e)
    // Disable audio for our movie.
    Movie.IsMuted = true;

Did you find this information useful? Please send us your suggestions and comments.

© Microsoft Corporation. All rights reserved.
© 2015 Microsoft