C# 如何让定时器只触发一次

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/982854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:51:32  来源:igfitidea点击:

How to make a timer fire only one time

c#.nettimer

提问by Madhu kiran

I am using C#2.0 and working on Winforms. I have two applications(app1,app2). when the app1 runs it will automatically invoke the app2. I have a timer in my app2 and in timer_tick I activate a buttonclick event.But I want this Button Click to be fired only one time when the app is started.

我正在使用 C#2.0 并在 Winforms 上工作。我有两个应用程序(app1,app2)。当 app1 运行时,它会自动调用 app2。我的 app2 和 timer_tick 中有一个计时器,我激活了一个 buttonclick 事件。但我希望这个 Button Click 在应用程序启动时只触发一次。

The problem i am facing is for some unknow reason the timer gets fired more than one time even though i make mytimer.Enable= false. Is there a way where i can make timer not be invoked second time. OR Is there a way i can make Button click event fired automatically without using timers.

我面临的问题是由于某种未知原因,即使我将 mytimer.Enable=false 设置为 false,计时器也会被触发多次。有没有办法让我的计时器不会被第二次调用。或者有没有一种方法可以让按钮点击事件在不使用计时器的情况下自动触发。

Here is the code:

这是代码:

private void Form1_Activated(object sender, EventArgs e)
{
    mytimer.Interval = 2000;
    mytimer.Enabled = true;
    mytimer.Tick += new System.EventHandler(timer1_Tick);

}
private void timer1_Tick(object sender, EventArgs e)
{
    mytimer.Enabled = false;
    button1_Click(this, EventArgs.Empty);
}


private void button1_Click(object sender, EventArgs e)
{ 
}

回答by Hugoware

You might try removing the handler rather than disabling the Timer

您可以尝试删除处理程序而不是禁用计时器

mytimer.Tick -= new System.EventHandler(timer1_Tick);

回答by BFree

public Form1 : Form()
{
   InitializeComponent();
   this.Load+= (o,e)=>{ this.button1.PerformClick();}
}

public void button1_Click(object sender, EventArgs e)
{
   //do what you gotta do
}

No need to use a timer. Just "click" the button when the form loads.

无需使用计时器。只需在表单加载时“单击”按钮即可。

回答by dverespey

Probably nothing to do with it, but I would set the timer enabled after I set EventHandler. (This has caused grief in a previous project where more code was inserted between the two statements later on.)

可能与它无关,但我会在设置 EventHandler 后设置计时器启用。(这在之前的项目中引起了悲痛,后来在两个语句之间插入了更多代码。)

回答by AllenG

I haven't tested this, yet (so be ready for an edit), but I suspect because you're enabling the timer (mytimer.Enabled = true;) in the Form1_Activated event instead of when the form initially loads. So every time the the Form becomes active, it resets Enables your timer.

我尚未对此进行测试(因此请准备好进行编辑),但我怀疑是因为您mytimer.Enabled = true;在 Form1_Activated 事件中启用了计时器 ( ),而不是在表单最初加载时启用。因此,每次表单变为活动状态时,它都会重置启用您的计时器。

EDIT: Okay, I have now verified: Assuming you do actually need the timer, move the mytimer.Enabled into the form's constructor.

编辑:好的,我现在已经验证:假设您确实需要计时器,请将 mytimer.Enabled 移动到表单的构造函数中。

回答by Martijn

Set the AutoResetproperty of the timer to false: http://msdn.microsoft.com/en-us/library/system.timers.timer.autoreset.aspx

AutoReset计时器的属性设置为falsehttp: //msdn.microsoft.com/en-us/library/system.timers.timer.autoreset.aspx

private void Form1_Activated(object sender, EventArgs e)
{
    mytimer.Interval = 2000;
    mytimer.AutoReset = false;
    mytimer.Tick += new System.EventHandler(timer1_Tick);
    mytimer.start();
}

This also means you don't have to unset Enabled.

这也意味着您不必取消设置Enabled

private void timer1_Tick(object sender, EventArgs e)
{
    mytimer.Enabled = false;
    button1_Click(this, EventArgs.Empty);
}