Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 107815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:48:48+00:00 2026-05-11T01:48:48+00:00

I currently have a database that gets updated from a legacy application. I’d like

  • 0

I currently have a database that gets updated from a legacy application. I’d like to utilize a SQL Service Broker queue so that when a record is updated, a message is placed in the queue (using a trigger or something).

I would then like to have a long running application (Windows service written in .NET) that is constantly ‘listening’ to the queue to grab the messages and process them for another application.

I’ve found some sample code on the web, and just wanted to get some input on whether the code is solid or not. So, here’s an abbreviated version of the Windows service class:

public class MyService {     public void StartService()     {         Thread listener = new Thread(Listen);         listener.IsBackground = true;         listener.Start();     }      private void Listen()     {         while (true)         {             using (SqlConnection connection = new SqlConnection(_connectionString))             {                 string commandText = 'WAITFOR ( RECEIVE * FROM MyQueue);';                 using (SqlCommand command = new SqlCommand(commandText, connection))                 {                     connection.Open();                     command.CommandTimeout = 0;                     SqlDataReader reader = command.ExecuteReader();                     while (reader.Read())                     {                         // Process message                     }                 }             }         }     } } 

What do you think? The code works exactly the way I want it to. But the idea of spinning off a new thread that contains a SQL command will never time out – inside an infinite loop – makes me a little nervous.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:48:49+00:00Added an answer on May 11, 2026 at 1:48 am

    Your service will not shutdown cleanly if you are in a infinite loop. You should be checking for an exit condition, set on when the service gets the shutdown message. You can add a timeout to the WAITFOR so that you can check if you should be shutting down. You should also check this on each row processed.

    I have used a 2 second sleep for the delay, but doing a 2 second timeout on the WAITFOR would accomplish the same thing.

    A service has 30 seconds to shutdown or Windows will consider it stuck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a Postgres 8.4 database that contains a varchar(10000) column. I'd like
I have a database that is currently sharded. This is the first time that
I have a database that contains data for many clients. Currently, we insert tens
I'm trying to normalize a mysql database.... I currently have a table that contains
I currently have a bunch of SQLCHAR objects from a database query. The query
I currently have a legacy system that uses SPs exclusively for access to the
Currently I have database with the following associations: One Client to Many Intakes One
Currently I have a database with the following relationships: One Client has multiple Entities
I currently have a MySQL database which I was hoping to use to store
I currently have a problem attepting to update a record within my database. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.