Re: C# CreateTimerQueueTimer
What's the actual C declaration of the function you're calling?
Re: C# CreateTimerQueueTimer
BOOL WINAPI CreateTimerQueueTimer(
__out PHANDLE phNewTimer,
__in_opt HANDLE TimerQueue,
__in WAITORTIMERCALLBACK Callback,
__in_opt PVOID Parameter,
__in DWORD DueTime,
__in DWORD Period,
__in ULONG Flags
);
Re: C# CreateTimerQueueTimer
Instead of passing a ref int*, which is an int**, you should pass an IntPtr.
Code:
private static unsafe extern bool CreateTimerQueueTimer(IntPtr phNewTimer, int TimerQueue....
Re: C# CreateTimerQueueTimer
Hi,
Unfortunately now it exceptions on the CreateTimerQueueTimer line.
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
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(IntPtr 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)
{
IntPtr pointer = new IntPtr();
int handle = CreateTimerQueue();
WaitOrTimerCallback callback = new WaitOrTimerCallback(EventCallback);
object whatever = 0;
bool result = CreateTimerQueueTimer(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);
}
Re: C# CreateTimerQueueTimer
I'm not sure, but maybe it easier to use .Net Frameworks ThreadPool.RegisterWaitForSingleObject?
- petter
Re: C# CreateTimerQueueTimer
Doesn't that have the same awful precision as DateTime.Now comparisons? It's something like 10 ms terribly inaccurately done, while the CreateTimer one is much more precise.
Re: C# CreateTimerQueueTimer
AFAIK CreateTimerQueueTimer isn't regarded a high-resolution timer:
Quote:
Originally Posted by MSDN on CreateTimerQueueTimer
If the DueTime and Period parameters are both nonzero, the timer will be signaled first at the due time, then periodically. The callback is called every time the period elapses, whether or not the previous callback has finished executing. Callback functions are queued to the thread pool. These threads are subject to scheduling delays, so the timing can vary depending on what else is happening in the application or the system.
- petter
Re: C# CreateTimerQueueTimer
Do you have any solutions then? At least I should be able to use CreateTimerQueue timer to test it. I used setTimerQueue, which tested effectively at 1 ms timer, but it exceptioned randomly after about 30 seconds about writing to protected memory. I then discovered the timer was obsolete, and replaced by the one I put here.
Re: C# CreateTimerQueueTimer
How accurate do you need? Are we talking microsecond accuracy? Millisecond accuracy? Nanosecond accuracy?
Re: C# CreateTimerQueueTimer
Probably microseconds is sufficient.