locked
is windows service threaded? RRS feed

  • Question

  • Hi,

    If I have SqlConnection or anyother code as Windows Service, is it automatically threaded by Windows or I should consider threading it?

    Thanks,

    Jassim

    Friday, December 25, 2015 5:52 PM

Answers

  • Hi Jassim,

    >>is it automatically threaded by Windows or I should consider threading it?

    In a Windows Service, we can stop and start the service from C# code, for example, we can write the service start from 20:00 and stop to 24:00 using C# code, so it will automatically threaded by Windows.

    And you don't need to consider threading it.  If you want to stop it. Just follow my screencapture as below.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Kristin Xie Tuesday, January 5, 2016 1:47 AM
    • Marked as answer by DotNet Wang Wednesday, January 6, 2016 1:17 AM
    Monday, December 28, 2015 2:19 AM
  • Nothing is automatically threaded just because it is running in a service. If you want threading then you have to add it in. But to clarify services almost always have a worker thread anyway because of the nature of how services work. Therefore you won't necessarily create a separate thread to talk to the DB (although you can use async) because your service already needs to be threaded.

    The problem is in OnStart. When the SCM is told to start your service it ultimately will call OnStart. This method must return within a short period of time.  If it doesn't then the SCM considers the service to be non-responsive and won't move it to started. Hence every implementation of OnStart will pretty much follow the same algorithm:

    Optionally ask for more time to start from the SCM
    Do any initialization work to ensure the service can start
    Create a worker thread to run the core logic of the service
    Return control to the SCM so it moves the service to started

    OnStop works in a similar manner:

    Notify the worker thread to terminate (event, cancellation token, etc)
    Wait for worker thread to terminate
    Return control to the SCM so it moves the service to stopped

    Since the core work of a service is already running in a worker thread you don't necessarily need to create separate threads for work unless you are trying to do work in parallel. If you do want to do work in parallel then consider using async/await instead of Task or spawning a worker thread.  Much of the core framework has been updated to support sync.

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

    • Proposed as answer by Kristin Xie Tuesday, January 5, 2016 1:47 AM
    • Marked as answer by DotNet Wang Wednesday, January 6, 2016 1:17 AM
    Wednesday, December 30, 2015 3:56 PM

All replies

  • Hi Jassim,

    >>is it automatically threaded by Windows or I should consider threading it?

    In a Windows Service, we can stop and start the service from C# code, for example, we can write the service start from 20:00 and stop to 24:00 using C# code, so it will automatically threaded by Windows.

    And you don't need to consider threading it.  If you want to stop it. Just follow my screencapture as below.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Kristin Xie Tuesday, January 5, 2016 1:47 AM
    • Marked as answer by DotNet Wang Wednesday, January 6, 2016 1:17 AM
    Monday, December 28, 2015 2:19 AM
  • Nothing is automatically threaded just because it is running in a service. If you want threading then you have to add it in. But to clarify services almost always have a worker thread anyway because of the nature of how services work. Therefore you won't necessarily create a separate thread to talk to the DB (although you can use async) because your service already needs to be threaded.

    The problem is in OnStart. When the SCM is told to start your service it ultimately will call OnStart. This method must return within a short period of time.  If it doesn't then the SCM considers the service to be non-responsive and won't move it to started. Hence every implementation of OnStart will pretty much follow the same algorithm:

    Optionally ask for more time to start from the SCM
    Do any initialization work to ensure the service can start
    Create a worker thread to run the core logic of the service
    Return control to the SCM so it moves the service to started

    OnStop works in a similar manner:

    Notify the worker thread to terminate (event, cancellation token, etc)
    Wait for worker thread to terminate
    Return control to the SCM so it moves the service to stopped

    Since the core work of a service is already running in a worker thread you don't necessarily need to create separate threads for work unless you are trying to do work in parallel. If you do want to do work in parallel then consider using async/await instead of Task or spawning a worker thread.  Much of the core framework has been updated to support sync.

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

    • Proposed as answer by Kristin Xie Tuesday, January 5, 2016 1:47 AM
    • Marked as answer by DotNet Wang Wednesday, January 6, 2016 1:17 AM
    Wednesday, December 30, 2015 3:56 PM