The process called each time when eventTimer_Elapsed. But I want to run it immediately(not to wait until timer will elapsed) when service started or Continued.
However I wanted to call it asynchronously from OnStart method, because otherwise Start command seems to hang until process is finished.
Thanks to asyncronous calls for delegates, I ‘ve created a function to invoke eventTimer_Elapsed from any place asyncronously and my service has the following methods:
private void BeginInvokeElapsedEvent(object sender, ElapsedEventArgs e)
{// Initialize the first update asynchronously
ElapsedEventHandler dlgt = eventTimer_Elapsed;
dlgt.BeginInvoke(sender,e);
}
protected override void OnContinue()
{
this.eventTimer.Start();
BeginInvokeElapsedEvent(this,null);// MyLongProcess();
base.OnContinue ();
}
protected override void OnStart(string[] args)
{
Initialise();
// Initialize the first update asynchronously// MyLongProcess();
BeginInvokeElapsedEvent(this,null);
}
private void eventTimer_Elapsed(object sender, ElapsedEventArgs e)
{
MyLongProcess();
}
Related links:Comparing the Timer Classes in the .NET Framework Class Library