Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everybody,

I need to execute a method a certain number of times per second. I tried to use three approaches, which didn't work.
I tried:
1. The DispatcherTimer,
2. The Thread.Sleep method (in this case, I executed the method which calls the method which have to be called x times per second in a new thread)
3. The System.Threading.Timer class.

None of them work properly.
If I put a number of 50 ops/s, i t takes 3 seconds to complete a progressbar (100 calls to the method UpdateProgressBar, which increase the value of the progress of 1 unit) instead of 2 seconds.
If I put 100 or 1000, it is the same, but if I put 1001, it is completed immediately.
I measured the time the method takes to call UpdateProgressBar 100 times and I found out that's about 1ms, so the 1 second error is not justified. I imagine that the timer interval property does not accept small values like 1.66666 milliseconds, or 20 milliseconds might be equal to 30 milliseconds... I don't know...

Can you help me to find the best approach to this problem?

Thanks a lot!!

Jymmy097
Posted
Comments
PIEBALDconsult 26-Jan-14 12:58pm    
Why?

You are trying to achieve something which is bound to be potentially unreliable, no matter how you do it. This is not a real-time OS. You cannot guarantee that your operation completes in a time frame shorted than the time slice you want to dedicate to it. You can improve your changes by adding priority to your thread and/or to your process, but those are only changes.

If you explain the ultimate goal of your activity, you can get a change for some useful advice. :-)

—SA
 
Share this answer
 
First, CodeProject is your friend: "Microsecond and Millisecond C# Timer" [^] (April, 2013). I think the content in that article can be adapted to meet your needs, since the author is dealing with a case where 0.8 ms. second accuracy (most of the time) is needed.

Without knowing a great deal more about what the method you need to execute 20 times per second is, and its behavior (callbacks ?), and the context of its use (threading ? explicit use of #n cores on a multi-core cpu ?) it's difficult to say much except that you can definitely exclude using the System.Windows.Forms.Timer (limited to about 55ms. accuracy, or less, I read somewhere).

Given the System.Threading.Timer uses the ThreadPool, and is commonly not used in WinForm applications, using that one may be questionable here.

System.Timers.Timer seems like the best candidate for your use; have you tried that ? The author of the article cited suggests its accuracy is about 15ms. which would meet your needs.

Given what you know (or can estimate) of the time it will take to execute the code in the method, are you sure your current hardware/software configuration is capable of executing the method 20 times per second ?

Will the method always take the same exact time to execute ?
 
Share this answer
 
Comments
LLLLGGGG 28-Jan-14 13:21pm    
I have solved my problem, but I haven't solved it yet. I have done a workaround: instead of doing n operations per second, I told me that the user doesn't know the exact number of ops/s, so I can emulate whatever I want without make the user know exaclty what I'm doing, and I'm satisfied even if that wasn't what I wanted. I wanted to emulate a CPU... I thought about multi-core cpu and I've added the property and a for loop.

Thanks for the help!!

Jymmy097

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900