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

  • Home
  • SEARCH
  • 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 3361726
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:11:15+00:00 2026-05-18T03:11:15+00:00

I’m currently building a Windows Service which needs to process a queue of messages

  • 0

I’m currently building a Windows Service which needs to process a queue of messages that are sat in a database table. This queue could vary in length and could take anything from 5 seconds to 55 seconds to execute against all rows in the database (I’m currently using a test data set of 500,000 records)

The Windows Service is configured to run on a 30 second timer so I have tried, unsuccessfully, to ensure that when the timer delegate runs that it is not able to run again until the previous request to the method has completed successfully

I have the following code in my Windows Service OnStart method:

     AutoResetEvent autoEvent = new AutoResetEvent(false);
     TimerCallback timerDelegate = new TimerCallback(MessageQueue.ProcessQueue);

     Timer stateTimer = new Timer(timerDelegate, autoEvent, 1000, Settings.Default.TimerInterval); // TimerInterval is 30000

     autoEvent.WaitOne();

And the following code in MessageQueue.ProcessMessage:

      Trace.Write("Starting ProcessQueue");
      SmtpClient smtp = new SmtpClient("winprev-01");

      AutoResetEvent autoEvent = (AutoResetEvent)stateObject;

      foreach (MessageQueue message in AllUnprocessed)
      {
          switch (message.MessageType)
          {
              case MessageType.PlainText:
              case MessageType.HTML:
                  SendEmail(smtp, message);

                  break;

              case MessageType.SMS:
                  SendSms(message);

                  break;

              default:
                  break;
          }
      }

      autoEvent.Set();
      Trace.Write("Ending ProcessQueue");

I’m using DebugView to analyse the view the Trace statements as the Service runs and I can see multiple instances of “Starting ProcessQueue” which occur every 30 seconds which is what I am trying to avoid happening

In summary: I want to call ProcessQueue and ensure that it is not executed again unless it has completed its work (this enables me to prevent the same messages in the queue being processed multiple times

I’m sure I’m missing something pretty obvious here so any help would be much appreciated 🙂

Dave

  • 1 1 Answer
  • 0 Views
  • 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. Editorial Team
    Editorial Team
    2026-05-18T03:11:16+00:00Added an answer on May 18, 2026 at 3:11 am

    Why don’t you have your delegate disable the timer and then re-enable it (or continue working, if timer would expire immediately) once it’s through working. Provided the latency between timer firing and your delegate waking up is < 30 seconds, this should be watertight.

    while (true)
    {
      Trace.Write("Starting ProcessQueue")
      stateTimer.Enabled = false;
      DateTime start = DateTime.Now;
    
      // do the work
    
      // check if timer should be restarted, and for how long
      TimeSpan workTime = DateTime.Now - start;
      double seconds = workTime.TotalSeconds;
      if (seconds > 30)
      {
        // do the work again
        continue;
      }
      else
      {
         // Restart timer to pop at the appropriate time from now
         stateTimer.Interval = 30 - seconds;
         stateTimer.Enabled = true;
         break;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.