我如何创建计时器循环,以便每t秒执行一次相同的功能 [英] How do i create timer loop so that every t seconds the same function is performed

查看:97
本文介绍了我如何创建计时器循环,以便每t秒执行一次相同的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

t.Enabled = true;
            while (t.Enabled == true)
            {
                scroll(telep.Handle, (speedslider.Value * 2));
            }


t是我的计时器. scroll(telep.Handle, (speedslider.Value * 2));是非常长且无聊的功能的缩写,几乎用滚动条的值乘以两(2)滚动滚动条.
使用while (t.Enabled == true)并不是很好,因为如果我尝试在滚动时与表单进行交互,那么表单会在几秒钟内变得无响应,然后一直滚动到底部.

我当时在考虑使用计时器来滚动2个像素,比如说30毫秒,但是我对计时器知之甚少.

如果有人可以将我带入正确的方向,或者甚至完全告诉我该怎么做,那将很棒.

感谢所有回答.:thumbsup:


where t is my timer. scroll(telep.Handle, (speedslider.Value * 2)); is short for a very very long and boring function, that pretty much scrolls the scrollbar by the value of a track bar multiplied by two (2).
Using while (t.Enabled == true) is not very good because if I try to interact with the form while it is scrolling, the form becomes unresponsive for a sec, then is scrolled all the way to the bottom.

I was thinking of using a timer for scrolling 2 pixels every, lets say for e.g., 30 milliseconds, but I do not know much about timers.

If someone could throw me into the correct direction, or even completely tell me how to do this, that would be great.

thanks to all who answer.:thumbsup:

推荐答案

嗨 您可以很好地使用Dylan的解决方案.那应该工作

另一种方法(但是不建议用户使用UI)……
如下例所示使用Threading计时器...不会使您的表单无响应,因为该计时器将位于其他线程中.

Hi You can use very well Dylan''s solution. That should work

Alternatively (However not recommended for a UI thred)......
Use the Threading timer as shown in the example below...it won''t make your form unresponsive, because the timer will be in a different thread.

public partial class Form1 : Form
 {
     private static int Count = 0;
     public Form1()
     {
         InitializeComponent();
     }

     private void Form1_Load(object sender, EventArgs e)
     {
         System.Threading.Timer timer = new System.Threading.Timer(new System.Threading.TimerCallback(MyIntervalFunction));
         timer.Change(0, 2000);
     }
     private void MyIntervalFunction(object obj){
         Count++;
         if (label1.InvokeRequired == true)
         {
             this.Invoke((MethodInvoker)delegate
             {
                 label1.Text = Count.ToString();
             });
         }
         else
         {
             label1.Text = Count.ToString();
         }
     }
 }



计数将添加到标签上,但您的表单仍会响应.这是一个示例,您可以根据需要使用MyIntervalFunction



The count will be added to the label, still your form will be responsive.This is an example and you can use the MyIntervalFunction for your requirement


设置计时器间隔属性,然后创建一个事件计时器滴答功能的处理程序.

完成!

Set the timers interval property, then create an event handler for the timers tick function.

Done!

private Timer timer = new Timer();

public void Init()
{
    timer.Interval = 5000; // every 5 seconds
    timer.Tick += new EventHandler(timer_Tick);
    timer.Enabled = true;
}
void timer_Tick(object sender, EventArgs e)
{
    Console.WriteLine("This event will happen every 5 seconds");
}


这篇关于我如何创建计时器循环,以便每t秒执行一次相同的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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