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 8719017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:41:14+00:00 2026-06-13T06:41:14+00:00

I have a windows service that can host one or more background threads. Each

  • 0

I have a windows service that can host one or more background threads. Each background thread starts a polling loop to check for work. I’ve been using the following pattern to implement the loop:

new Thread(DoWork).Start();

private void DoWork()
{
    while(keepRunning)
    {
        Console.WriteLine(DateTime.Now);
        Thread.Sleep(1000);
    }
}

I’m trying to find an alternative that will consume less CPU. I’ve been able to achieve a polling loop using a Timer with a recursive method:

var timer = new Timer(WakeUp, null, Timeout.Infinite, 1000)
timer.Change(0, 1000);

private void WakeUp(object state)
{
    lock (locker)
        Monitor.Pulse(locker);
}

private void DoWork()
{
    lock (locker)
    {
        if (!keepRunning) return;
        Console.WriteLine(DateTime.Now);
        Monitor.Wait(locker);
        DoWork();
    }
}

Are there any advantages to using one pattern over the other? Is there a better pattern for setting up a polling loop in a background thread?

  • 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-06-13T06:41:16+00:00Added an answer on June 13, 2026 at 6:41 am

    I’m trying to find an alternative that will consume less CPU.

    Do you have any evidence that it’s actually consuming a significant amount of CPU at the moment? Unless you’re got a lot of threads which are waking up at similar times and contending, I’d expect Thread.Sleep to be reasonably efficient.

    Your recursive DoWork method is far worse – unless it’s tail-call optimized, you’ll eventually blow up with a stack overflow. Why aren’t you at least looping? Why don’t you just do the actual work in the timer thread to start with?

    If you really want one thread per task, doing some work once per second, then your first code is okay (although you’ll want to make sure you “see” any new values of keepRunning if it’s just a field – consider using Interlocked). Ideally though, you shouldn’t have those threads at all – just have appropriate timers to schedule the work once per second, and let the thread pool handle it.

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

Sidebar

Related Questions

I have Windows Service written in C#. It starts two threads, one is pooling
I have a windows service that starts a host for a WCF service. I
I have an ASP.NET web service that I can access via a windows program
I have a system service running on my Windows machine that can impersonate the
I have a windows service that does some intensive work every one minute (actually
I am doing a windows service that must have network connectivity when it starts.
I have a Windows service that writes messages to the Event Log. I also
I have a Windows service that runs as a logged-in user (local admin). During
I have a Windows Service that takes the name of a bunch of files
I have a windows service that generates logs as it does some execution. 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.