C#计时器在若干次滴答声后自动停止
如何在几次滴答声或3-4秒后停止计时器 所以我启动了一个计时器,我想在10点后或2-3秒后自动停止C#计时器在若干次滴答声后自动停止,c#,winforms,timer,C#,Winforms,Timer,如何在几次滴答声或3-4秒后停止计时器 所以我启动了一个计时器,我想在10点后或2-3秒后自动停止 谢谢 您可以像 int counter = 0; 然后在每一个刻度中增加它。在您的限制之后,您可以停止计时器。在你的勾号事件中这样做 counter++; if(counter ==10) //or whatever your limit is yourtimer.Stop(); 假设您使用的是System.Windows.Forms.Tick。你可以跟踪一个计数器,以及它的生存
谢谢 您可以像
int counter = 0;
然后在每一个刻度中增加它。在您的限制之后,您可以停止计时器。在你的勾号事件中这样做
counter++;
if(counter ==10) //or whatever your limit is
yourtimer.Stop();
假设您使用的是
System.Windows.Forms.Tick
。你可以跟踪一个计数器,以及它的生存时间。这是使用计时器的标记属性的好方法。
这使得它可用于其他计时器,并使代码保持通用性,而不是为每个计时器使用全局定义的int计数器
此代码是安静的通用代码,因为您可以指定此事件处理程序来管理它的生存时间,并指定另一个事件处理程序来处理为其创建计时器的特定操作
System.Windows.Forms.Timer ExampleTimer = new System.Windows.Forms.Timer();
ExampleTimer.Tag = new CustomTimerStruct
{
Counter = 0,
StartDateTime = DateTime.Now,
MaximumSecondsToLive = 10,
MaximumTicksToLive = 4
};
//Note the order of assigning the handlers. As this is the order they are executed.
ExampleTimer.Tick += Generic_Tick;
ExampleTimer.Tick += Work_Tick;
ExampleTimer.Interval = 1;
ExampleTimer.Start();
public struct CustomTimerStruct
{
public uint Counter;
public DateTime StartDateTime;
public uint MaximumSecondsToLive;
public uint MaximumTicksToLive;
}
void Generic_Tick(object sender, EventArgs e)
{
System.Windows.Forms.Timer thisTimer = sender as System.Windows.Forms.Timer;
CustomTimerStruct TimerInfo = (CustomTimerStruct)thisTimer.Tag;
TimerInfo.Counter++;
//Stop the timer based on its number of ticks
if (TimerInfo.Counter > TimerInfo.MaximumTicksToLive) thisTimer.Stop();
//Stops the timer based on the time its alive
if (DateTime.Now.Subtract(TimerInfo.StartDateTime).TotalSeconds > TimerInfo.MaximumSecondsToLive) thisTimer.Stop();
}
void Work_Tick(object sender, EventArgs e)
{
//Do work specifically for this timer
}
我通常说,因为你没有提到哪个计时器,但它们都有滴答声。。。因此:
在课堂上你需要一个计数器,比如
int count;
您将在计时器开始时初始化它,并且需要一个日期时间,如
DateTime start;
您将在计时器开始时对其进行初始化:
start = DateTime.Now;
在您的勾选方法中,您将执行以下操作:
if(count++ == 10 || (DateTime.Now - start).TotalSeconds > 2)
timer.stop()
这里有一个完整的例子
public partial class meClass : Form
{
private System.Windows.Forms.Timer t;
private int count;
private DateTime start;
public meClass()
{
t = new Timer();
t.Interval = 50;
t.Tick += new EventHandler(t_Tick);
count = 0;
start = DateTime.Now;
t.Start();
}
void t_Tick(object sender, EventArgs e)
{
if (count++ >= 10 || (DateTime.Now - start).TotalSeconds > 10)
{
t.Stop();
}
// do your stuff
}
}
当达到计时器的指定间隔(3秒后)时,将调用事件处理程序timer1\u Tick()
event handler,您可以在事件处理程序中停止计时器
Timer timer1 = new Timer();
timer1.Interval = 3000;
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(timer1_Tick);
void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop(); // or timer1.Enabled = false;
}
初始化计时器时,将标记值设置为0(零)
然后,每打一个勾,加一个
tmrAutoStop.Tag = int.Parse(tmrAutoStop.Tag.ToString()) + 1;
并检查它是否达到您想要的数字:
if (int.Parse(tmrAutoStop.Tag.ToString()) >= 10)
{
//do timer cleanup
}
使用相同的技术替换与计时器关联的事件:
if (int.Parse(tmrAutoStop.Tag.ToString()) % 2 == 0)
{
//do something...
}
else
{
//do something else...
}
要检查经过的时间(以秒为单位):
C#:-)中有这么多计时器,哪个计时器(哪个类/名称空间)。至少有5个计时器:system.windows.forms.timer如果您不累的话,您可以查看我的答案;)当达到计时器的指定间隔时(3秒后),将调用timer1_Tick()事件处理程序,您可以在事件处理程序中停止计时器。
if (int.Parse(tmrAutoStop.Tag.ToString()) % 2 == 0)
{
//do something...
}
else
{
//do something else...
}
int m = int.Parse(tmrAutoStop.Tag.ToString()) * (1000 / tmrAutoStop.Interval);