有没有一种方法来延迟事件处理程序(比如1秒)的Windows窗体 [英] Is there a way to delay an event handler (say for 1 sec) in Windows Forms

查看:134
本文介绍了有没有一种方法来延迟事件处理程序(比如1秒)的Windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够延迟事件处理某些控件(如按钮),以1秒的实际事件之后被解雇,例如(点击事件为例)..这可能由.NET Framework?

我用一个计时器,并叫我从计时器的Tick事件code如下,但我不知道这是最好的办法!

 无效onButtonClick(..)
{
   timer1.Enabled = TRUE;
}

无效onTimerTick(..)
{
   timer.Enabled = FALSE;

   CallMy codeNow();
}
 

解决方案

也许你可以使创建计时器的方法?

 无效onButtonClick(对象发件人,EventArgs的)
{
    延迟(1000,(O,A)=>的MessageBox.show(测试));
}

静态无效延时(INT MS,事件处理程序的动作)
{
    VAR TMP =新的Timer {间隔=毫秒};
    tmp.Tick + =新的EventHandler((O,E)=> tmp.Enabled = FALSE);
    tmp.Tick + =行动;
    tmp.Enabled = TRUE;
}
 

I need to be able to delay the event handlers for some controls (like a button) to be fired for example after 1 sec of the actual event (click event for example) .. is this possible by the .net framework ?

I use a timer and call my code from the timer's tick event as below but I am not sure if this is the best approach !

void onButtonClick( ..)
{
   timer1.Enabled = true;
}

void onTimerTick( ..)
{
   timer.Enabled = false; 

   CallMyCodeNow();
}

解决方案

Perhaps you could make a method that creates the timer?

void onButtonClick(object sender, EventArgs e)
{
    Delay(1000, (o,a) => MessageBox.Show("Test"));
}

static void Delay(int ms, EventHandler action)
{
    var tmp = new Timer {Interval = ms};
    tmp.Tick += new EventHandler((o, e) => tmp.Enabled = false);
    tmp.Tick += action;
    tmp.Enabled = true;
}

这篇关于有没有一种方法来延迟事件处理程序(比如1秒)的Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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