Call eventTimer_Elapsed method asyncronoulsy OnStart of Windows Service

We have   eventTimer in Windows Service  (similar as described in  Timer Objects in Windows Services with C#.NET 
and Using Timers in a Windows Service) to run the  relatively long-running process repeatedly.
 
NOTE: It is NOT a good idea to Use a Windows Service just to run a scheduled process, but I have to maintain a legacy application, that was written as a Windows Service .
 
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
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Sunday, June 07, 2009 3:41 AM
Print

Comments on this entry:

# re: Call eventTimer_Elapsed method asyncronoulsy OnStart of Windows Service

Left by ayman at 4/3/2011 2:34 AM
Gravatar
can I Calling Method when oprating system windows start ??

# re: Call eventTimer_Elapsed method asyncronoulsy OnStart of Windows Service

Left by Michael Freidgeim at 5/13/2011 6:23 AM
Gravatar
ayman,
windows services are started when windows is started, or you can schedule task to run when windows is started,

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910