timSetEvent in C# - Passing object to static function

J

Jeffrey B. Holtz

Has anyone used the multimedia timere timeSetEvent in C#? I'm trying
to use it to get a 1ms accurate timer. All the other implementations
are far to inaccurate in their resolution. 1ms Timer = 0ms.....60ms.
I'm already timing the callbacks using the Win32API
QueryPerformanceCounter/Frequency. The code I have is actually
performing the callback and it looks like it is close to 1ms but I now
need to get my object passed to this static event so I can make some
instance calls. Can anyone help? I know that I could do this in
standard C or C++ by passing the class as a pointer when I register
the Timer Callback (the lUserData parameter to timeSetEvent):

public sealed class Win32TimingFunctions
{
public delegate void TIMECALLBACK(uint wTimerID, uint msg, uint
dwUser, uint dw1, uint dw2);
public struct TIMECAPS
{
public uint wPeriodMin; /* minimum period supported */
public uint wPeriodMax; /* maximum period supported */
}

public enum EVENT_TYPE : uint
{
TIME_ONESHOT = 0x0000, /* program timer for single event */
TIME_PERIODIC = 0x0001 /* program for continuous periodic event */
}
public enum CALLBACK_TYPE : uint
{
TIME_CALLBACK_FUNCTION = 0x0000, /* callback is function */
TIME_CALLBACK_EVENT_SET = 0x0010, /* callback is event - use
SetEvent */
TIME_CALLBACK_EVENT_PULSE = 0x0020 /* callback is event - use
PulseEvent */
}

[DllImport("winmm.dll", CallingConvention=CallingConvention.Winapi)]
public extern static uint timeSetEvent(uint uDelay, uint
uResolution, TIMECALLBACK lpFunction, IntPtr dwUser, EVENT_TYPE
uFlags);
[DllImport("winmm.dll", CallingConvention=CallingConvention.Winapi)]
public extern static uint timeKillEvent(uint uTimerID);
}

public class TimerClass
{
...
private int m_nInstanceNumber = 0;
private static uint m_snTimerID = uint.MaxValue;
public void StartTimer()
{
if (m_snTimerID == uint.MaxValue)
{
Win32TimingFunctions.EVENT_TYPE uTypeEvent;
uint uElapse = 1;
uint uResolution = 0;
IntPtr lUserData = IntPtr.Zero;
m_callback = new
Win32TimingFunctions.TIMECALLBACK(TIMECALLBACK_SendMessage);
m_snMsgsToSend = (uint)numUpDownNumMsgs.Value;
m_snMsgsSent = 0;

if (numUpDownNumMsgs.Value > 1)
uTypeEvent = Win32TimingFunctions.EVENT_TYPE.TIME_PERIODIC;
else
uTypeEvent = Win32TimingFunctions.EVENT_TYPE.TIME_ONESHOT;
// Set minimum System timing
Win32TimingFunctions.timeBeginPeriod(1);

// Setup Callback function
m_snTimerID = Win32TimingFunctions.timeSetEvent(
uElapse, //period (in milliseconds)
uResolution, //resolution (0=maximum)
m_callback, //callback function
lUserData, //no user data
uTypeEvent); //periodic timer
}
else
{
Win32TimingFunctions.timeKillEvent(m_snTimerID);
m_snTimerID = uint.MaxValue;
}
}
static void TIMECALLBACK_SendMessage(uint wTimerID, uint msg, uint
dwUser, uint dw1, uint dw2)
{
// What I'd like to do
TimerClass pInstance = dwUser as TimerClass;
pInstance.InstanceMethod();
}
public bool InstanceMethod()
{
Console.WriteLine("Member variable {0}",m_InstanceNumber);
}
...
}

Thanks for any help or direction.

Jeff Holtz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top