Oli_c

Hello,

I search how to create a timer on Xbox and XNA. On Win32 there is a System.Timers.Timer object but it doesn't exist on Xbox.

[DllImport("kernel32")]

public static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency);

[DllImport("kernel32")]

public static extern bool QueryPerformanceCounter(ref long PerformanceCount);

My application need a hight precision timer, because I need to get the reaction time of the user. I have also tested with the kernell32.dll and but it doesn't exist on Xbox.

My first idea will be to create a GameComponent and in the Update method to get TotalRealTime and use a modulo :

gameTime.TotalRealTime.TotalMilliseconds % TickPerMilliseconds;

And if the result equals zero I generate an event;

Does a better solution exist

Thank you for your help.

OC



Re: XNA Game Studio Express How to create a Hight Precision Timer on Xbox

Oli_c

I have tested my solution, it doesn't work because the Update function is called every 15ms and I need a call every 1ms.

Can you help me

OC





Re: XNA Game Studio Express How to create a Hight Precision Timer on Xbox

Shawn Hargreaves - MSFT

You could use a StopWatch, or (better) you could use the parameters on the Game object to control how fast your Update tick runs.





Re: XNA Game Studio Express How to create a Hight Precision Timer on Xbox

Oli_c

Ok,

But the problem is that we can't use StopWach with an event and to define tick per seconds. StopWach must use pooling and if the Update function isn't run every 1ms, this timer isn't an high precision.

It's necessary to be sure that Update function can be run every 1ms or less. But also without decreasing the performances.

Thank you for your help.

OC





Re: XNA Game Studio Express How to create a Hight Precision Timer on Xbox

Oli_c

I have found a solution, I use the System.Threading.Thread. I post here :

using System;

using System.Collections.Generic;

using System.Text;

using System.Threading;

using System.Diagnostics;

namespace XNALibrary.Timers

{

public class RepetiveTimer

{

protected int interval;

protected TimerCallback timerDelegate;

protected System.Threading.Timer timer;

protected Stopwatch stopwatch;

#region Constructors

public RepetiveTimer()

{

Initialize(System.Threading.Timeout.Infinite);

}

public RepetiveTimer(int interval)

{

Initialize(interval);

}

public void Initialize(int interval)

{

// Create the stopwatch for the elapsed time

this.stopwatch = new Stopwatch();

this.interval = interval;

// Create the delegate that invokes methods for the timer.

this.timerDelegate = new TimerCallback(OnElapsedEvent);

this.timer = new System.Threading.Timer(timerDelegate, this, 0, interval);

}

#endregion Constructors

#region Event

/// <summary>

/// Event for Timer with the elapsed time

/// </summary>

[Serializable]

public class TimerEventArgs : EventArgs

{

private readonly double elapsedTime;

public TimerEventArgs(double elapsedTime)

: base()

{

this.elapsedTime = elapsedTime;

}

public double ElapsedTime

{

get { return elapsedTime; }

}

}

/// <summary>

/// Delegate to redirect ticks

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

public delegate void ElapsedHandler(object sender, TimerEventArgs e);

/// <summary>

/// Event that save the invoke method by the ElapsedHandler delegate

/// </summary>

public event ElapsedHandler Elapsed;

/// <summary>

/// Invoked method by the Timer

/// </summary>

/// <param name="sender"></param>

protected virtual void OnElapsedEvent(Object sender)

{

if (Elapsed != null)

{

TimerEventArgs evt = new TimerEventArgs(stopwatch.ElapsedMilliseconds);

Elapsed(sender, evt);

}

}

#endregion Event

#region Properties

/// <summary>

/// Interval Property

/// </summary>

public int Interval

{

get { return interval; }

set { interval = value; }

}

#endregion

#region Timer Functions

/// <summary>

/// Start the timer and use the interval member variable

/// </summary>

public virtual void Start()

{

stopwatch.Start();

timer.Change(Interval, Interval);

}

/// <summary>

/// Start the timer and specify the interval

/// In Millisec

/// </summary>

/// <param name="interval"></param>

public virtual void Start(int interval)

{

Interval = interval;

stopwatch.Start();

timer.Change(Interval, Interval);

}

/// <summary>

/// Pause the timer

/// </summary>

public virtual void Pause()

{

stopwatch.Stop();

timer.Change(0, System.Threading.Timeout.Infinite);

}

/// <summary>

/// Stop the timer

/// </summary>

public virtual void Stop()

{

stopwatch.Reset();

timer.Change(0, System.Threading.Timeout.Infinite);

}

/// <summary>

/// Dispose the timer

/// </summary>

public virtual void Dispose()

{

timer.Dispose();

}

#endregion

}

public class OneShotTimer : RepetiveTimer

{

public OneShotTimer()

: base()

{

}

protected override void OnElapsedEvent(object sender)

{

base.OnElapsedEvent(sender);

base.Stop();

}

}

}