I'm trying to make a periodic timer but this thing seriously just does not work. Maybe I'm getting the wrong signature when I try and use it in C#? It fires the callback immediately (despite it not being supposed to), then gets a memory write exception to 0x00000000.
NewTimer timer = new NewTimer(1000);
Code:class NewTimer { DateTime start; int ctr = 0; [DllImport("kernel32.dll")] private unsafe static extern int CreateTimerQueue(); [DllImport("kernel32.dll")] private static unsafe extern bool CreateTimerQueueTimer(ref int* phNewTimer, int TimerQueue, WaitOrTimerCallback Callback, object Parameter, long DueTime, long Period, uint Flags); private delegate void TimeProc(long param, bool timeOrWaitFired); internal unsafe NewTimer(long millis) { int queueHandler; int* pointer = &queueHandler; int handle = CreateTimerQueue(); WaitOrTimerCallback callback = new WaitOrTimerCallback(EventCallback); object whatever = 0; bool result = CreateTimerQueueTimer(ref pointer, handle, callback, whatever, 10000, 10000, 0); start = DateTime.Now; } public void EventCallback(object param, bool timeOrWaitFired) { ctr++; DateTime now = DateTime.Now; TimeSpan diff = now - start; Console.WriteLine(ctr / diff.TotalSeconds); } }




Reply With Quote