Nerd Herder

Dean Johnson blogs about life on the XNA platform and tools team

Calling Guide.IsTrialMode on Windows Phone 7

Calling Guide.IsTrialMode on Windows Phone 7

  • Comments 2

As you may have heard calling Guide.IsTrialMode can take around 60 milliseconds to return and thus should not be called each frame in your game. Unfortunately this performance penalty only occurs when your game is downloaded from the WP7 marketplace and not while you are in your development environment so you won't see that calls to Guide.IsTrialMode is slowing down your game’s performance.

So you may be wondering when should you call Guide.IsTrialMode. You could check at startup or game activation and then cache the value but this means your game is depending on the game exiting or deactivating when you call Guide.ShowMarketplace.

A better approach is to check the trial state on a background thread and cache the state for polling on the main thread. If the game is still in trial mode then the background thread can sleep for awhile until it wakes up and checks the state again.

The TrialModeManager singleton class below implements this trial caching on a separate thread and can be used in your games to allow you to poll the trial state without hitting the 60 milliseconds penalty.

/// <summary>
/// Simple singleton class that will check the state of trial mode on a background thread
/// </summary>
public class TrialModeManager
{
    /// <summary>
    /// Property to get the one and only instance of this class
    /// </summary>
    public static TrialModeManager Instance 
    {
        get
        {
            if (instance == null)
            {
                instance = new TrialModeManager();
            }
            return instance;
        }
    }
    private static TrialModeManager instance;

    /// <summary>
    /// Returns the trial state
    /// </summary>
    public bool TrialMode
    {
        get
        {
            return trialMode;
        }
    }
    // Default to trial mode
    private volatile bool trialMode = true;

    // The background thread
    private Thread trialThread;

    /// <summary>
    /// Simple constructor that create the background thread and starts it up
    /// </summary>
    private TrialModeManager()
    {
        trialThread = new Thread(CheckTrial);
        trialThread.IsBackground = true;
        trialThread.Start();
        // Wait for the thread to start up
        while (!trialThread.IsAlive);
    }

    /// <summary>
    /// The thread function that will check the trial state in the background
    /// </summary>
    private void CheckTrial()
    {
        // Stop the thread once we are no longer in trial since the state can't change anymore
        while (trialMode)
        {
            trialMode = Guide.IsTrialMode;

            // Sleep if the game is in trial 
            if (trialMode)
            {
                Thread.Sleep(TimeSpan.FromSeconds(3));
            }
        }
    }
}

Now in your game when you need to check if the game is in trial mode call TrialModeManager.Instance.TrialMode and you will not see a slowdown in your game.

  • Is there some similar performance impact on XBox?

  • No.

Page 1 of 1 (2 items)
Leave a Comment
  • Please add 3 and 5 and type the answer here:
  • Post
Translate This Page
Search
Archive