windows service with Lon running process in C#


hi,

i have long running task  which have created on onstart method of service takes time , status of services "starting" , display error message. work going on, takes 2 or 3 hours.

and need same task after time interval again , again on time.

i have written function on onstart ..

please suggest how use threading , other asynchronous method task fetching data sql server , doing manipulation , using sdk inserting data .

thanks  

a service required of work on background thread.  scm call onstart method , has few seconds respond (although can ask delay, not recommended).  therefore onstart methods spawn worker thread work while main thread returns.  if you're getting error scm onstart method isn't doing this.  critical onstart method not block.  unlike other app model you'll run across service code (your worker thread) continue run.

if need repeat work on , on again recommend in onstart method start system.timers.timer.  timer needs field in service class lifetime extends life of service.  timer should start , can continually call worker method @ intervals (but see below).  when timer elapses raise event on worker thread call worker method work.  don't need involved threading or tasks encapsulated.

one caveat of timer worker method must reentrant.  every time timer elapses method called if still running previous expiration.  if cannot handle reentrancy (or don't want it) not set timer autoreset.  instead add block worker method starts timer again when you're done.

this model has been done many people in past, in forums, can google many, many examples of how implement this.  here's couple of starter links:

http://clifgriffin.com/using-timers-in-a-c-windows-service/
http://www.informit.com/articles/article.aspx?p=415982&seqnum=2

michael taylor
http://msmvps.com/blogs/p3net



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'