|
-
February 27th, 2008, 01:16 PM
#1
C# CreateTimerQueueTimer
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);
}
}
-
February 27th, 2008, 01:40 PM
#2
Re: C# CreateTimerQueueTimer
What's the actual C declaration of the function you're calling?
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
February 27th, 2008, 02:56 PM
#3
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
);
-
February 27th, 2008, 03:43 PM
#4
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....
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
February 27th, 2008, 04:15 PM
#5
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);
}
-
February 27th, 2008, 04:52 PM
#6
Re: C# CreateTimerQueueTimer
I'm not sure, but maybe it easier to use .Net Frameworks ThreadPool.RegisterWaitForSingleObject?
- petter
-
February 27th, 2008, 04:56 PM
#7
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.
-
February 27th, 2008, 05:29 PM
#8
Re: C# CreateTimerQueueTimer
AFAIK CreateTimerQueueTimer isn't regarded a high-resolution timer:
 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
-
February 28th, 2008, 09:15 AM
#9
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.
-
February 28th, 2008, 11:24 AM
#10
Re: C# CreateTimerQueueTimer
How accurate do you need? Are we talking microsecond accuracy? Millisecond accuracy? Nanosecond accuracy?
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
February 29th, 2008, 11:45 AM
#11
Re: C# CreateTimerQueueTimer
Probably microseconds is sufficient.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|