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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:46:38+00:00 2026-05-13T23:46:38+00:00

I’m working on a Windows Service that’s having some issues with Thread.Sleep() so I

  • 0

I’m working on a Windows Service that’s having some issues with Thread.Sleep() so I figured I would try to use a timer instead as this question recommends:

Using Thread.Sleep() in a Windows Service

Thing is it’s not entirely clear to me how one might implement this. I believe this is the way but I just wanted to make sure:

'' Inside The Service Object
Dim closingGate As System.Threading.AutoResetEvent

Protected Overrides Sub OnStart(ByVal args() As String)
   Dim worker As New Threading.Thread(AddressOf Work)
   worker.Start()
End Sub

Protected Sub Work()
   Dim Program = New MyProgram()
   closingGate = New System.Threading.AutoResetEvent(False)
   Program.RunService(closingGate)
End Sub

Protected Overrides Sub OnStop()
   closingGate.Set()
End Sub

'' Inside My Programs Code:
Public Sub RunService(closingGate As AutoResetEvent)
   Dim tmr As New Timer
   '' ...and so on

   closingGate.WaitOne()
End Sub

Aside from using VB.Net (Kidding, but I’d be using C# if I could.) am I on the right track here? Is this better than using Thread.Sleep()?

  • 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-13T23:46:39+00:00Added an answer on May 13, 2026 at 11:46 pm

    Honestly, I would have to say that I think you’re a little off the beaten path here.

    First of all, the actual code posted doesn’t really make sense; the worker thread is waiting for a signal but for no reason – it’s not actually in a loop of any kind or waiting for a resource. Second, if you did need to execute some (perhaps omitted) cleanup code in the worker after receiving the shutdown signal, your service might not give the worker thread enough time to clean up.

    But, more fundamentally, all you’ve done is move the problem to a separate thread. This may keep the service responsive to the service controller but it doesn’t fix the design problem – and it adds a lot of unnecessary complexity by using the threads and mutexes; ultimately it’s just going to make your service harder to debug if you ever need to.

    Let’s say you need to execute some code every 5 seconds. The idea is, instead of “waiting” 5 seconds, you invert the control, and let the timer invoke your work.

    This is bad:

    protected override void OnStart(string[] args)
    {
        while (true)
        {
            RunScheduledTasks();    // This is your "work"
            Thread.Sleep(5000);
        }
    }
    

    This is good:

    public class MyService : ServiceBase
    {
        private Timer workTimer;    // System.Threading.Timer
    
        protected override void OnStart(string[] args)
        {
            workTimer = new Timer(new TimerCallback(DoWork), null, 5000, 5000);
            base.OnStart(args);
        }
    
        protected override void OnStop()
        {
            workTimer.Dispose();
            base.OnStop();
        }
    
        private void DoWork(object state)
        {
            RunScheduledTasks();  // Do some work
        }
    }
    

    That’s it. That’s all you have to do to do work at regular intervals. As long as the work itself doesn’t run for seconds at a time, your service will remain responsive to the service controller. You can even support pausing under this scenario:

    protected override void OnPause()
    {
        workTimer.Change(Timeout.Infinite, Timeout.Infinite);
        base.OnPause();
    }
    
    protected override void OnContinue()
    {
        workTimer.Change(0, 5000);
        base.OnContinue();
    }
    

    The only time when you need to start creating worker threads in your service is when the work itself can run for a very long time and you need the ability to cancel in the middle. That tends to involve a good deal of design effort, so I won’t get into that without knowing for sure that it’s related to your scenario.

    It’s best not to start introducing multithreaded semantics into your service unless you really need it. If you’re just trying to schedule small-ish units of work to be done, you definitely don’t need it.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want use html5's new tag to play a wav file (currently only supported
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
We're building an app, our first using Rails 3, and we're having to build
I have some data like this: 1 2 3 4 5 9 2 6
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

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.