我可以在一个应用程序中使用多少计时器 [英] How many timers can i use in one application

查看:95
本文介绍了我可以在一个应用程序中使用多少计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试用C#创建GUI。我对计时器有一点怀疑。我可以在一个应用程序中使用多少计时器。因为我试图每秒更新表或网格中的不同传感器值。我在我的应用程序中使用了大约5个计时器。运行1小时后我发现我的应用程序运行速度非常慢,并且值不会更新,因为它应该在前一个小时内更新。



当我停止更新表格时,它一直都非常好用,并且正如我所知得到的值。



为什么我使用计时器意味着不同的传感器会在不同时间给出值,例如每秒一个传感器,另一个15秒或5秒等等。



有谁能建议我是计时器的问题?如果是这样,那么替换它们或管理它们的最佳解决方案是什么。

Hi,

I am trying to create GUI in C#. I have a small doubt about timers. How many timers can i use in one application. because I am trying to update different sensors values in table or grid at every sec. I used around 5 timers in my application. I found after running 1 hour my application running very slow and values are not updating as it should or not like in first one hour.

When i stop updating table then it is working very good for all the time and getting values as i should.

Why i have used timer means different sensors will give values at different times like one sensor on every sec and another on 15sec or 5 sec e.t.c

Can anyone suggest me is the problem with the timers? if so what will be the best solution to replace them or manage them.

推荐答案

我知道没有技术限制,但有更好的方法。



创建一个定时器,其间隔是其他定时器的最小公分母,例如,如果你有每隔1,5,10和15秒发生的事情,然后创建一个每秒钟滴答的计时器。然后做这样的事情:



There is no technical limit that I know of, however there is a better way.

Create one timer with an interval that is the lowest common denominator of the other ones, for example if you have something that happens every 1, 5, 10, and 15 seconds, then create one timer that ticks off every second. Then do something like this:

int count = 0;  //Place this as a global variable in your class

private void TimerTick(object sender)
{
    DoOneSecondActions();
    
    count++;

    if (count % 5 == 0)
        DoFiveSecondActions();

    if (count %15 == 0)
        DoFifteenSecondActions();   
}

private void DoOneSecondActions()
{
}

private void DoFiveSecondActions()
{
}

private void DoFifteenSecondActions()
{
}





你应该在计时器上发布你正在做的代码。使用诸如BeginInvoke之类的方法可能会导致UI缓慢下降,或者如果您不处理异常,导致堆栈增长等等。使用多个计时器的方法不应该减慢UI,这可能是您在正在进行的计时器滴答。



You should post the code of what you are doing in the timer. Using methods like BeginInvoke can cause slow-downs in the UI, or if you are not handling exceptions, causing the stack to grow, etc. Your approach with multiple timers should not slow down the UI, its probably something that you are doing in the timer ticks that is doing it.


首先,目前为止最好的建议是解决方案1.



在你的使用偶数2个定时器会有点滥用,有时候根本不建议使用定时器(单独的线程可以更好地工作)。但是,在你的情况下,计时器可能是一个更好的选择,但只有一个。所以,你最好的选择是这个建议。



定时器经常会产生很多问题。所以,另外我想建议我使用计时器进行数据库轮询的方案,这极大地提高了可靠性。请参阅我过去的回答:

使用计时器轮询数据库 [ ^ ] 。



-SA
First of all, the best advice so far is the Solution 1.

In your case, using even 2 timers would be somewhat of an abuse, and sometimes using a timer at all is not recommended (a separate thread would work much better). However, in your case, timer is probably really a better choice, but only one. So, your best bet is this advice.

Timers often creates many problems. So, additionally I want to advise my scheme of using a timer for database polling which greatly improves the reliability. Please see my past answer:
Polling Database with Timer[^].

—SA


从技术上讲,有一个限制等于USER对象的限制Windows强制执行的每个进程。定时器被认为是USER对象,以及由您的应用程序创建的任何和所有窗口,控件,字体,位图,笔,画笔,等等,等等等等。



从Windows NT开始,每个进程的USER对象限制为10,000,任何一个Session的限制为65,536。



说真的,如果你需要创建那么多的计时器,你的应用程序设计糟糕。
Technically, there is a limit equal to the limit of USER objects per process imposed by Windows. Timers are considered USER objects, as well as any and all windows, controls, fonts, bitmaps, pens, brushes, blah, blah, blah, created by your application.

Starting with Windows NT, the USER object limit per process is 10,000 with 65,536 the limit for any one Session.

Seriously, if you need to create that many timers, you have a terrible design for your app.


这篇关于我可以在一个应用程序中使用多少计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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