如何实现计时器的Tick事件? [英] How to implement Tick event to timer?

查看:415
本文介绍了如何实现计时器的Tick事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。



我正在尝试制作动态搜索文本框。对于搜索框,我使用timer和Textchanged事件。

我发现的方式是:懒惰模式,意味着保持文本的最后一次更改,然后在另一个线程(如计时器)检查是否有在上次用户更改为当前时间之间超过2秒,更新搜索:(计时器间隔为2秒)。



计时器的初始化程序,它位于构造函数中表格。

Hello.

I am trying to make a dynamic search text box. For the search box I use timer and Textchanged event.
The way i found out is: Lazy pattern, means keep the last time of text changed, then in another thread (like timer) check if there is more than 2 second between last user change to current time, update your search: (timer interval is 2 second).

Initializer of the timer and it is in the constructor of the form.

Timer timer = new Timer();

private void InitTimer()
{
    timer.Tick += new EventHandler(timer1_Tick);
    timer.Interval = 5000;
    timer.Start();
}



我在事件中使用的变量。


The variables I use in the events.

private DateTime lastChange = DateTime.Now;
private bool textChanged = false;
object lockObject = new object();



这是textchanged事件:


Here's the textchanged event:

private void textChanged(object sender, EventArg e)
{
   lock(lockObject)
   {
      lastChange = DateTime.Now;
      textChanged = true;
   }
}







private void timer1_Tick(object sender, EventArgs е)
{
    lock(lockObject)
    {
       if (textChanged && lastChange > DateTime.Now.AddSeconds(-2)) // wait 2 second for changes
       {
          DoSearch();
          textChanged = false;
          lastChange = DateTime.Now;
       }
    }
}

private void DoSearch() 
{ 
    timer.Stop();
    MessageBox.Show("results");
    //Do search
}



问题是,当我开始输入时,计时器启动并开始在屏幕周围显示一堆消息框。


The problem is that when I start typing the timer is started and begins showing bunch of message boxes around the screen.

推荐答案

嗯。

这不是你的代码,或者它没有编译,你没有注意到。

这个:

Um.
That isn't your code, or it doesn't compile, and you haven't noticed.
This:
private bool textChanged = false;

这:

This:

private void textChanged(object sender, EventArg e)
{
   ...

and

And

if (textChanged && lastChange > DateTime.Now.AddSeconds(-2)) // wait 2 second for changes
{
   ...

由于两个名称之间存在歧义,因此无法编译。 (并且它是 EventArgs ,而不是事件处理程序声明中的EventArg



所以首先让它进行编译,然后从那里开始工作。如果没有你实际运行的代码,我们什么也做不了!

Will not compile as there is an ambiguity between the two names. (And it's EventArgs, not EventArg in the event handler declaration.

So start by getting it to compile, and then work from there. We can't do anything without the code you are actually running!


这就是我的意思我的上述评论意味着:



Here, this is what I meant by my above comment:

Timer _InputTimer = new Timer();
string _Input = "";

private void _InitTimer()
{
    _InputTimer.Tick += new EventHandler(_InputTimer_Tick);
    _InputTimer.Interval = 2000;
    _InputTimer.Start();
}

private void _InputTimer_Tick(object sender, EventArgs е)
{
    if (_Input != txtInput.Text){
        _InputTimer.Stop();
        _Input = txtInput.Text;
        _DoSearch();
    }
}

private void _DoSearch()
{
    MessageBox.Show("Now's where you might multi-thread");
}


这篇关于如何实现计时器的Tick事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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