[英]Execute a Function before a Timer start
I am a bit new Here .I have window service in which I have a timer which executes a function after 1 Min interval..I want to Execute function first time before Timer start and then after each Timer Interval... 我在这里有点新。我有窗口服务,其中有一个计时器,该计时器在1分钟间隔后执行一个功能。.我想在计时器启动之前第一次执行功能,然后在每个计时器间隔之后执行...
here is My Code : 这是我的代码:
public partial class ASMSFetchService : ServiceBase
{
System.Timers.Timer updateAutoSMSTimer;
public ASMSFetchService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
updateAutoSMSTimer = new System.Timers.Timer(1 * 60 * 1000);
updateAutoSMSTimer.Elapsed += new System.Timers.ElapsedEventHandler(Slots);
updateAutoSMSTimer.Enabled = true;
updateAutoSMSTimer.AutoReset = true;
updateAutoSMSTimer.Start();
}
private void Slots(object sender, ElapsedEventArgs e)
{method1();}
private void method1()
{ //SomeOpeartion }
}
The function is executing when the 1 timer interval complete ...I want to call the method1() before timer start and then after every Timer interval .... 该函数在1个计时器间隔完成时正在执行...我想在计时器启动之前然后在每个计时器间隔之后调用method1()....
I have try to put method in the Start() and in the constructor() ..But it is not working ...I am not sure ..But Can it be possible ??? 我试图将方法放在Start()和Constructor()中..但是它不起作用...我不确定..但是有可能吗?
Any suggestion will be Helpful 任何建议都会有帮助
I want to call the method1() before timer start and then after every Timer interval
我想在计时器启动之前然后在每个计时器间隔之后调用method1()
Then call it before you start the timer 在启动计时器之前先调用它
method1();
updateAutoSMSTimer.Start();
try to call if before start then : 尝试在开始之前致电,然后:
protected override void OnStart(string[] args)
{
updateAutoSMSTimer = new System.Timers.Timer(1 * 60 * 1000);
updateAutoSMSTimer.Elapsed += new System.Timers.ElapsedEventHandler(Slots);
updateAutoSMSTimer.Enabled = true;
updateAutoSMSTimer.AutoReset = true;
method1();
updateAutoSMSTimer.Start();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.