Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com
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
Posted on Sunday, June 7, 2009 3:41 AM .Net Framework | Back to top


Comments on this post: Call eventTimer_Elapsed method asyncronoulsy OnStart of Windows Service

# re: Call eventTimer_Elapsed method asyncronoulsy OnStart of Windows Service
Requesting Gravatar...
can I Calling Method when oprating system windows start ??
Left by ayman on Apr 03, 2011 2:34 AM

# re: Call eventTimer_Elapsed method asyncronoulsy OnStart of Windows Service
Requesting Gravatar...
ayman,
windows services are started when windows is started, or you can schedule task to run when windows is started,
Left by Michael Freidgeim on May 13, 2011 6:23 AM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net