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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:49:18+00:00 2026-05-31T04:49:18+00:00

I understand that in a Windows service, it is better to use Timer rather

  • 0

I understand that in a Windows service, it is better to use Timer rather than Thread.Sleep(timeout). However, in all code examples I could find on the Internet that were handling Azure workers, it is Thread.Sleep(timeout) which is used instead of Timer.

Even the default code provided in the Worker project template in Visual Studio uses a Thread.Sleep:

public class WorkerRole : RoleEntryPoint
{
    public override void Run()
    {
        // This is a sample worker implementation. Replace with your logic.
        Trace.WriteLine("$projectname$ entry point called", "Information");

        while (true)
        {
            Thread.Sleep(10000);
            Trace.WriteLine("Working", "Information");
        }
    }
// ...
}

So far, I’ve been also using Thread.Sleep in my workers but without really understanding why. So my question is, why using Thread.Sleep(timeout) in Azure worker role rather than Timer? What is the difference between Windows service and Azure worker that leads to this difference in how we are supposed to conceive this kind of application? Is it good or bad to use Timer in Azure workers?

Any explanation with links to some resources explaining the fundamentals of this is welcome as I couldn’t find anything so far.

  • 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-31T04:49:20+00:00Added an answer on May 31, 2026 at 4:49 am

    The purpose of the Thread.Sleep() loop is to keep the Run() method from exiting. If Run() exits, then your worker will restart. I don’t know that you could accomplish that goal effectively with a Timer.

    Most likely your CPU is wasting some tiny amount of time to wake up that thread every 1000 msecs in order to do nothing. I doubt it’s significant, but it bugged me too. My solution was to wait on a CancellationToken instead.

    public class WorkerRole : RoleEntryPoint {
        CancellationTokenSource cancelSource = new CancellationTokenSource();
    
        public override void Run()
        {
            //do stuff
            cancelSource.Token.WaitHandle.WaitOne();
        }
    
        public override void OnStop()
        {
            cancelSource.Cancel();
        }
    }
    

    This keeps the Run() method from exiting without wasting CPU time on busy waiting. You can also use the CancellationToken elsewhere in your program to initiate any other shutdown operations you may need to perform.

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

Sidebar

Related Questions

I would like to use a timer instead of sleep within a windows service
I already have a C# windows service that we use internally to monitor a
I understand that Perl's OO model is rather primitive; it is, in most respects,
I am creating Windows service class in Python that will eventually display a Window
I've created a Windows Service that accepts commands from remote machines via WCF. One
I've create a Windows Service using C# that when OnStart is called creates a
I have a windows service that fails to start under some circumstances. When it
I'm troubleshooting an OutOfMemory exception in my .NET 2.0 Windows Service application. To understand
I'm trying to understand better how Windows sessions work, so if I have some
I am trying to understand better how Windows sessions (TS sessions and log on

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.