每秒执行一定数量的操作(调用方法) [英] Execute a certain number of operations (calls to a method) per second

查看:125
本文介绍了每秒执行一定数量的操作(调用方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要每秒执行一定次数的方法。我尝试使用三种方法,但没有用。

我试过:

1. DispatcherTimer,

2.线程。 Sleep方法(在这种情况下,我执行调用方法的方法,该方法必须在新线程中每秒调用x次)

3. System.Threading.Timer类。



它们都没有正常工作。

如果我输入50个操作数/秒,完成一个进度条需要3秒钟(100个调用方法UpdateProgressBar,它增加1个单位的进度值而不是2秒。

如果我把100或1000,它是相同的,但如果我把1001,它立即完成。

我测量了方法调用UpdateProgressBar 100次的时间,我发现大约1ms,所以1秒的错误是不合理的。我想,timer interval属性不接受像1.66666毫秒这样的小值,或者20毫秒可能等于30毫秒...我不知道...



你能帮我找到解决这个问题的最佳方法吗?



非常感谢!!



Jymmy097

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

推荐答案

无论你怎么做,你都在努力实现一些可能不可靠的东西。这不是一个实时操作系统。您不能保证您的操作在短于您想要专用的时间片的时间范围内完成。您可以通过为线程和/或流程添加优先级来改善您的更改,但这些只是更改。



如果您解释了活动的最终目标,你可以得到一些有用建议的改变。 :-)



-SA
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


首先,CodeProject是你的朋友:Microsecond和Millisecond C#计时器[ ^ ] (2013年4月)。我认为该文章中的内容可以根据您的需求进行调整,因为作者正在处理0.8毫秒的情况。需要第二个准确度(大部分时间)。



不知道你需要每秒执行20次的方法更多,它的行为(回调?),以及它的使用环境(线程?在多核cpu上明确使用#n核心?)除了你之外很难说绝对可以排除使用System.Windows.Forms.Timer(限制在约55ms。准确度,或更低,我在某处阅读)。



给出System.Threading。 Timer使用ThreadPool,并且通常不在WinForm应用程序中使用,在这里可能会有问题。



System.Timers.Timer似乎是你的最佳人选使用;你试过吗?引用文章的作者表示其准确度约为15ms。这将满足您的需求。



鉴于您知道(或可以估算)执行方法中的代码所需的时间,您确定当前的硬件/软件配置能够每秒执行20次这个方法吗?



方法总是会花费相同的时间执行吗?
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 ?

这篇关于每秒执行一定数量的操作(调用方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆