Timer, tickcount, ticks?

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 233
Reputation: Lord Soth is an unknown quantity at this point 
Solved Threads: 4
Lord Soth's Avatar
Lord Soth Lord Soth is offline Offline
Posting Whiz in Training

Re: Timer, tickcount, ticks?

 
0
  #11
Mar 31st, 2006
Hi,

You can dynamically adjust the delay amount by checking the tick count difference at each iteration thus obtaining perfect timing independent of processor load, chosen FPS or hardware bottlenecks. Put " System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.AboveNormal; " to the first line of CaptureLoop();

Loren Soth
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Timer, tickcount, ticks?

 
0
  #12
Mar 31st, 2006
I see... I've set the thread priority in a slightly different location so that the entire application runs at highest. Any downsides to that?


And while I understand what you're saying, the implementation you're suggesting isn't clear. I'll work on it and post the result to see if I'm going about this in an appropriate manner.
Explainer of control logic and some basics.
"If you seek to drink from a fountain of knowledge, make sure your cup is big enough."
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 233
Reputation: Lord Soth is an unknown quantity at this point 
Solved Threads: 4
Lord Soth's Avatar
Lord Soth Lord Soth is offline Offline
Posting Whiz in Training

Re: Timer, tickcount, ticks?

 
0
  #13
Apr 1st, 2006
Hi,

As long as the place you put the priority code is in the same thread as loop it won't matter. and for the delay here is a "still dynamic but not that much" kind of idea : let the loop rool for 4-5 times without delay and gather tickcount differences then average them to have see how much you code takes then Delay = (1000/FPS)-AverageCodeTimeInMSec;
Happy coding.

Loren Soth
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Solved Threads: 5
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: Timer, tickcount, ticks?

 
0
  #14
Apr 10th, 2006
Okay, this is what I've done...

  1. private void DelayAdjust()
  2. {
  3. System.Int64 frequ = 0;
  4. if (QueryPerformanceFrequency(ref frequ) != 0)
  5. {
  6.  
  7. dt.AllocateFrames(8);
  8. refTick=System.Environment.TickCount;
  9.  
  10. System.Int64 count1 = 0;
  11. System.Int64 count2 = 0;
  12. if (QueryPerformanceCounter(ref count1) != 0)
  13. {
  14.  
  15. for(int i = 0; i<8; i++)
  16. {
  17. curTick=System.Environment.TickCount; //int t
  18. this.Times.Add(System.DateTime.Now);
  19.  
  20. if (!dt.AcquireFrame(TotalFramesAcquired++))
  21. {
  22. TotalFramesAcquired--;
  23. }
  24. decimal oops = (curTick-refTick)/((TotalFramesAcquired + missed) *frameDelay); //formerly decimal oops
  25. ticks.Add(curTick);
  26.  
  27. if (oops > 1m)
  28. {
  29. missed+= Convert.ToInt32(Math.Round(oops,0));
  30. }
  31. Application.DoEvents();
  32. this.pbarCapture.Value=TotalFramesAcquired;
  33. }
  34. QueryPerformanceCounter(ref count2);
  35. //System.Int64 time_ms = (count2 - count1) * 1000 /(8* frequ);
  36. offset= Convert.ToInt32((count2 - count1) * 1000 /(8* frequ));
  37. this.lPerformance.Text="Delay(in ms): " +offset.ToString();//time_ms.ToString();
  38. }
  39. else
  40. {
  41. this.lPerformance.Text="Using preset delays";
  42. }
  43. }
  44. else
  45. this.lPerformance.Text="Using preset delays";
  46.  
  47. dt.AllocateFrames(3);
  48. ticks.Clear();
  49. Times.Clear();
  50. TotalFramesAcquired = 0;
  51. missed = 0;
  52. this.pbarCapture.Value=TotalFramesAcquired;
  53.  
  54. }

And it doesn't work very well.

When I run it, even if it reports that it is using the exact same offset as the funny equations I came up with, the capture runs take longer and miss more frames than when using presets.
This is rather annoying. I still haven't been able to get my boss to get ahold of his laptop so that I can see if the hard-coded version works acceptably on that, but I don't think this self-adjusting tick offset code will perform well.
Explainer of control logic and some basics.
"If you seek to drink from a fountain of knowledge, make sure your cup is big enough."
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum


Views: 19130 | Replies: 13
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC