469,140 Members | 1,326 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,140 developers. It's quick & easy.

timSetEvent in C# - Passing object to static function

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
[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_Sen dMessage);
m_snMsgsToSend = (uint)numUpDownNumMsgs.Value;
m_snMsgsSent = 0;

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

// 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
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;
public bool InstanceMethod()
Console.WriteLine("Member variable {0}",m_InstanceNumber);

Thanks for any help or direction.

Jeff Holtz
Nov 15 '05 #1
0 3068

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Steven T. Hatton | last post: by
4 posts views Thread by Ryan | last post: by
2 posts views Thread by =?Utf-8?B?VmljdG9yIExhaQ==?= | last post: by
12 posts views Thread by Bryan Parkoff | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.