有没有一种方法来延迟事件处理程序(比如1秒)的Windows窗体窗体、方法来、事件、程序

2023-09-03 10:13:10 作者:对岸.

我需要能够延迟事件处理某些控件(如按钮),以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;
}
 

DOTA2的TI比赛开始了,没想到却是以这样的方式上了热搜

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;
}

 
精彩推荐
图片推荐