.net 如何使用计时器停止名称本身重复10000次?
如何防止名称重复10000次。。现在它每5秒重复一次。。但在24小时内,它最多只能重复10000次。。如何在给定时间停止计时器 节目--:.net 如何使用计时器停止名称本身重复10000次?,.net,c#-4.0,console-application,.net,C# 4.0,Console Application,如何防止名称重复10000次。。现在它每5秒重复一次。。但在24小时内,它最多只能重复10000次。。如何在给定时间停止计时器 节目--: 添加一个变量以跟踪其被激发的次数(count),并在每次激发时递增该变量。一旦达到10000(或任何其他数字),通过stop()将计时器的状态设置为stopped 根据您的要求使用计数器,而不是时间计算。如果您需要它每x分钟或x秒运行一次,那么计算时间是可行的。现在,您希望它运行10000次,然后在每次Elasped事件提升时递增计数器。第10000次时,通
添加一个变量以跟踪其被激发的次数(
count
),并在每次激发时递增该变量。一旦达到10000(或任何其他数字),通过stop()
将计时器的状态设置为stopped
根据您的要求使用计数器,而不是时间计算。如果您需要它每x分钟或x秒运行一次,那么计算时间是可行的。现在,您希望它运行10000次,然后在每次Elasped事件提升时递增计数器。第10000次时,通过
aTimer.Enabled=false
或aTimer.Stop()
禁用Elasped事件触发
班级计时器1
{
公共系统定时器定时器定时器;
公共静态计数器
public static void Main()
{
counter = 0;
System.Timers.Timer aTimer;
aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 5000;
aTimer.Enabled = true;
Console.WriteLine("Press Enter to Exit the Program");
Console.ReadLine();
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
counter = counter + 1;
Console.WriteLine("Name YaP {0}", e.SignalTime);
if(counter == 10000)
aTimer.Enabled = false;
}
}
我希望这能帮助你,你的答案总是乐于助人!
请访问下面的链接以获取更多帮助
//http://msdn.microsoft.com/en-us/library/system.timers.elapsedeventargs.signaltime.aspx 嘿,格雷格,非常感谢你。这确实帮了我很大的忙。我能保持联系以获得进一步的帮助吗?
class Timer1
{
public System.Timers.Timer aTimer;
public static int count; // Keeps track of how many times its been fired
public static void Main()
{
count = 0; // Initialize to zero
System.Timers.Timer aTimer;
aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 5000;
aTimer.Enabled = true;
Console.WriteLine("Press Enter to Exit the Program");
Console.ReadLine();
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
if(count >= 10000) // Check
{
aTimer.stop(); // Stop the timer
}
Console.WriteLine("Name YaP {0}", e.SignalTime);
++count;
}
}
public static void Main()
{
counter = 0;
System.Timers.Timer aTimer;
aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 5000;
aTimer.Enabled = true;
Console.WriteLine("Press Enter to Exit the Program");
Console.ReadLine();
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
counter = counter + 1;
Console.WriteLine("Name YaP {0}", e.SignalTime);
if(counter == 10000)
aTimer.Enabled = false;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
namespace Time_Writer
{
class Program
{
static int count = 1;
static double seconds
private static System.Timers.Timer aTimer;
static void Main(string[] args)
{
aTimer = new System.Timers.Timer();
aTimer.Elapsed += new System.Timers.ElapsedEventHandler(aTimer_Elapsed);
aTimer.Interval = 8600;
aTimer.Enabled = true;
Console.WriteLine("Press Enter to Exit the Program\n");
Console.ReadLine();
}
private static void aTimer_Elapsed(object source,ElapsedEventArgs e)
{
Console.WriteLine("Current time is :{0}\n ", e.SignalTime.TimeOfDay.ToString());
seconds += 8.6;
count += 1;
if (count > 5 || seconds == 43) //change count to 10000, seconds to 86400
{
aTimer.Enabled = false;
Console.WriteLine("Timer is off at {0}\n\n",e.SignalTime.TimeOfDay.ToString());
//Reset variables and re-enable the timer if you desire to.
count = 0;
seconds = 0;
aTimer.Enabled = true;
}
}
}
}