none
Windows Service RRS feed

  • Question

  • Hi Experts,

    Am still in learning phase of C#.  How can I create a Windows service continuously running in background 

    Performing

    1. Query SQL Server_1 table every 30 second.

    2. Store the result in memory

    3. After next run discard the old result and save new result in memory

    4. The stored result in memory will be used to create multiple child processes of again querying other instances of SQL Servers

    Let say,

    SQL Server_1 table contains

    1. SQL Server_N Server Details & Query to be executed on them. 

    2. Background process will read data from this table in SQL Server_1 every 30 seconds.

    3. The result may contain 2-5 records. Each record in itself need to spawn a new child worker thread asynchronously performing query on the SQL Server instance details available in the result loaded into the memory for next 30 seconds continuously with a delay of based on Repeat_Frequency

    Table example 

    SQL Server_1 SQL_Server_Instance_Details Query Repeat_Frquency
      LocalHost_1 Insert INTO Table 2 values ('Xyz','Abc') Every 5 second
      Localhost_2 Insert INTO Table3 values ('ABC',123) Every 3 second

    Windows scheduler is not an option as it can't go under 1 minute. This design pattern I will be using for plethora of other tasks. Am just expressing it high level.

    Apologies for such requirement. It will be supportive if solution for the same can be shared. 

    Thanks in advance

    Priya

    Monday, October 21, 2019 4:26 AM

Answers

All replies

  • Hi Priya,

    I think it best if you post some of the code you've already started on and we can then help you more from there. We need more to go on.

    I can, at the very least, point you to a blog post I wrote that gives you some ideas of how to start writing a Windows Service. The key is to begin by hosting your Service(s) in a Console Application. The transition from tinkering with it while you’re developing, to deploying it as a Windows Service, is made a lot easier this way.

    Here's a link to my blog post:

    https://geek-goddess-bonnie.blogspot.com/2013/10/easy-windows-services.html


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, October 21, 2019 5:14 AM
    Moderator
  • Hi Priya,

    I think it best if you post some of the code you've already started on and we can then help you more from there. We need more to go on.

    I can, at the very least, point you to a blog post I wrote that gives you some ideas of how to start writing a Windows Service. The key is to begin by hosting your Service(s) in a Console Application. The transition from tinkering with it while you’re developing, to deploying it as a Windows Service, is made a lot easier this way.

    Here's a link to my blog post:

    https://geek-goddess-bonnie.blogspot.com/2013/10/easy-windows-services.html


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Hello, 

    Surely, Thanks a lot for the shared details.

    Am currently trying to figure out , how can I create child processes also part of the windows service to execute asynchronously based on data read from the SQL Server. Could you please share some possible approaches. 

    Thanks 


    • Edited by Priya Bange Monday, October 21, 2019 12:29 PM .
    Monday, October 21, 2019 12:28 PM
  • Hello,

    I have a Microsoft TechNet article with source code included.

    • Shows how to start/stop a Window service using a custom app.
    • Has the ability via app.config to do work
    • Inserts data into a SQL-Server database table.
    • Explains how to debug, if debugging on Win 10 see my instructions on TechNet.

    The above should give you a good foundation for working with Window Services but is not a comprehensive guide.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Priya Bange Tuesday, October 22, 2019 2:37 AM
    Monday, October 21, 2019 12:43 PM
    Moderator
  • Hello,

    I have a Microsoft TechNet article with source code included.

    • Shows how to start/stop a Window service using a custom app.
    • Has the ability via app.config to do work
    • Inserts data into a SQL-Server database table.
    • Explains how to debug, if debugging on Win 10 see my instructions on TechNet.

    The above should give you a good foundation for working with Window Services but is not a comprehensive guide.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thanks a ton for the shared details. It will definitely support my project :)

    I got an error while trying to access the shared link for debugging

    <text>></text> 
    Gallery

    This item is not yet published.

    If you are the owner of this project, please sign in with the appropriate account.

    Thanks again

    Priya

     
    Monday, October 21, 2019 1:16 PM
  • Am currently trying to figure out , how can I create child processes also part of the windows service to execute asynchronously based on data read from the SQL Server. Could you please share some possible approaches. 



    You bet! I have a few more blog posts on spinning off asynchronous threads that might give you some ideas:
     
    https://geek-goddess-bonnie.blogspot.com/2017/03/fire-and-forget.html
    https://geek-goddess-bonnie.blogspot.com/2018/05/asynchronicity.html
    https://geek-goddess-bonnie.blogspot.com/2019/06/long-running-process-on-timer.html


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Priya Bange Tuesday, October 22, 2019 2:37 AM
    Monday, October 21, 2019 3:46 PM
    Moderator
  • Hello,

    I have a Microsoft TechNet article with source code included.

    • Shows how to start/stop a Window service using a custom app.
    • Has the ability via app.config to do work
    • Inserts data into a SQL-Server database table.
    • Explains how to debug, if debugging on Win 10 see my instructions on TechNet.

    The above should give you a good foundation for working with Window Services but is not a comprehensive guide.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thanks a ton for the shared details. It will definitely support my project :)

    I got an error while trying to access the shared link for debugging

    <text>></text> 
    Gallery

    This item is not yet published.

    If you are the owner of this project, please sign in with the appropriate account.

    Thanks again

    Priya

     

    Try this link 

    https://social.technet.microsoft.com/wiki/contents/articles/52152.windows-service-developer-gui-for-install-while-in-development-mode.aspx


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, October 22, 2019 9:33 AM
    Moderator