CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2004
    Posts
    38

    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);
    		}
    	}

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    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.

  3. #3
    Join Date
    May 2004
    Posts
    38

    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
    );

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    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.

  5. #5
    Join Date
    May 2004
    Posts
    38

    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);
    		}

  6. #6
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: C# CreateTimerQueueTimer

    I'm not sure, but maybe it easier to use .Net Frameworks ThreadPool.RegisterWaitForSingleObject?

    - petter

  7. #7
    Join Date
    May 2004
    Posts
    38

    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.

  8. #8
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    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

  9. #9
    Join Date
    May 2004
    Posts
    38

    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.

  10. #10
    Join Date
    May 2007
    Posts
    1,546

    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.

  11. #11
    Join Date
    May 2004
    Posts
    38

    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
  •  





Click Here to Expand Forum to Full Width

Featured