如何等待计时器直到它完成多个间隔 [英] how to wait for timer until it completes a number of intervals

查看:78
本文介绍了如何等待计时器直到它完成多个间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有问题,我想运行一个间隔为20毫秒的计时器,但方法(methodA)调用它并启动它。需要计时器在10次执行后完成任务(意味着任务的总时间为200毫秒)。



i希望方法A等待请执行10个计时器。



任何想法,或者什么是最合适的解决方案。



谢谢

hi guys,
i have a problem please, i want to run a timer with 20 milliseconds interval but a method (methodA) calls it and start it. it is needed that timer should complete its task after 10 execution ( means that the total time will be 200 millisecond for a task).

i want that methodA should wait for the 10 execution of the timer please.

any idea please, or what could be the best proper solution.

thanks

推荐答案

简单!创建一个类级别计数器,将其设置为迭代次数,并在Tick事件中对其进行计数:

Easy! Create a class level counter, set it to the number of iterations, and count it down in the Tick event:
    Timer myTimer = new Timer();
    myTimer.Interval = 20;
    myTimer.Tick += new EventHandler(myTimer_Tick);
    intervalCounter = 10;
    myTimer.Start();
    }
private int intervalCounter;

void myTimer_Tick(object sender, EventArgs e)
    {
    if (intervalCounter > 0)
        {
        intervalCounter--;
        if (intervalCounter == 0)
            {
            // Do what you want
            intervalCounter = 10;
            }
        }
    }


这篇关于如何等待计时器直到它完成多个间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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