如何使计时器只发射一次

时间:2022-09-11 18:47:43

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事件。但我希望这个按钮点击只在应用程序启动时被触发一次。

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,计时器也会被触发多次。有没有办法我可以让计时器不被第二次调用。或者有没有办法让我可以自动触发按钮点击事件而不使用定时器。

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)
{ 
}

5 个解决方案

#1


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.

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

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

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

#2


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.

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

#3


You might try removing the handler rather than disabling the Timer

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

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

#4


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后设置定时器。 (这引起了之前项目的悲痛,后来在两个语句之间插入了更多代码。)

#5


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

将计时器的AutoReset属性设置为false:http://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.

这也意味着您不必取消设置已启用。

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

#1


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.

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

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

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

#2


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.

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

#3


You might try removing the handler rather than disabling the Timer

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

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

#4


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后设置定时器。 (这引起了之前项目的悲痛,后来在两个语句之间插入了更多代码。)

#5


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

将计时器的AutoReset属性设置为false:http://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.

这也意味着您不必取消设置已启用。

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