none
Windows Service with a thread RRS feed

  • Question

  • Hi I have a windows service written using .Net 4.0 that on the whole works fine, at the end of this services I want it to start an asynchronous processes and this is not working. 

    if I call the function normally it works fine. 

    i do not get any errors but the processes does nor run anyone have any idea's why 

    the code to start the processes is quite simple 

    Dim t As New System.Threading.Thread(AddressOf import.StartImport)
    t.Start()


    Regards Dagz

    Friday, June 19, 2015 4:23 PM

Answers

  • To be clear a service must be threaded, you cannot wait until the end.  When a service is started by the SCM and the OnStart method is called you only have a limited time to return back to the SCM. As such you pretty much have to spawn a thread that runs for the life of your service and does the real work of the service.

    When the SCM finally tells the service to terminate your OnStop will be called and you have a limited amount of time to terminate your worker thread and return. At this point you cannot start any additional work as your process is going to be terminated (in most cases).

    The code you gave should be in your OnStart method.  After you start your worker thread OnStart returns and the SCM assumes your service is running.  Your worker thread cannot terminate.  It must run until told to stop.  That generally happens by your OnStop method raising an event that tells the worker to terminate.  The worker thread listens for this event, cleans itself up and exits.  Once your OnStop method knows the thread has terminated it can return to the SCM which will terminate your process.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by Dagz Friday, June 19, 2015 6:43 PM
    Friday, June 19, 2015 4:38 PM
    Moderator

All replies

  • To be clear a service must be threaded, you cannot wait until the end.  When a service is started by the SCM and the OnStart method is called you only have a limited time to return back to the SCM. As such you pretty much have to spawn a thread that runs for the life of your service and does the real work of the service.

    When the SCM finally tells the service to terminate your OnStop will be called and you have a limited amount of time to terminate your worker thread and return. At this point you cannot start any additional work as your process is going to be terminated (in most cases).

    The code you gave should be in your OnStart method.  After you start your worker thread OnStart returns and the SCM assumes your service is running.  Your worker thread cannot terminate.  It must run until told to stop.  That generally happens by your OnStop method raising an event that tells the worker to terminate.  The worker thread listens for this event, cleans itself up and exits.  Once your OnStop method knows the thread has terminated it can return to the SCM which will terminate your process.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Marked as answer by Dagz Friday, June 19, 2015 6:43 PM
    Friday, June 19, 2015 4:38 PM
    Moderator
  • ok thanks i see the error of my ways, i have to give this some thought as the service collects files then imports so I will need to consider the best way to work around this. 


    Regards Dagz

    Friday, June 19, 2015 6:44 PM