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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:25:27+00:00 2026-06-18T07:25:27+00:00

I have small windows service which should write some data to web service from

  • 0

I have small windows service which should write some data to web service from time of day to time of day, in regular interval which user sets up.

For ex. from 8:00 till 22:00 every 20 minutes

But after few times 10 or so triggering the callback, timer just stops, there is nothing in Event log and service is working fine meaning no exception crashed him.

Code is quite simple:

private Dictionary<string, Timer> m_timers = new Dictionary<string, Timer>();

protected override void OnStart(string[] args)
{
      // load settings

      foreach(var settings in userSettings)
      {
         SetUpTimer(settings);
      }
}

private void Callback(Settings settings)
{
    try
    {
       //Write some data to web service
       //Write to event log that web service write suceeded
    }
    catch(Exception ex)
    {
       //Write to event log
    }

    SetUpTimer(settings);
}

private void SetUpTimer(Settings settings)
{
    Timer timer;

    if (m_timers.ContainsKey(settings.Name))
    {
        timer = m_timers.Where(x => x.Key == settings.Name).Select(x => x.Value).FirstOrDefault();
        // Dispose timer (there is rumor that it can slide after few days), just to be sure.

        if (timer != null)
            timer.Dispose();
    }

    TimeSpan timeToFirstRun = settings.TimeFrom - DateTime.Now.TimeOfDay;

    while (timeToFirstRun.TotalHours < 0 || timeToFirstRun > settings.TimeTo)
    {
        timeToFirstRun += TimeSpan.FromMinutes(settings.EveryHowMuch);
    }

    if (timeToFirstRun > settings.TimeTo)
        timeToFirstRun = settings.TimeFrom - DateTime.Now.TimeOfDay + TimeSpan.FromDays(1.0);

    timer = new Timer(Callback, settings, timeToFirstRun, new TimeSpan(-1));

    if (!m_timers.ContainsKey(settings.Name))
    {
        m_timers.Add(settings.Name, timer);
    }

    // Write to event log that it sucessfully set up for next callback
}
  • 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-18T07:25:29+00:00Added an answer on June 18, 2026 at 7:25 am

    Well, you have a really big flaw here:

    You dispose a timer, but you don’t remove it from m_timers. The newly created timer with this name will never be added to the dictionary and will, as a consequence, get collected by the GC at some point.

    Furthermore, you should be using TryGetValue instead of ContainsKey in conjunction with a LINQ query.

    The top part of your method should look something like this:

    Timer timer;
    if(m_timers.TryGetValue(settings.Name, out timer))
    {
        timer.Dispose();
        m_timers.Remove(settings.Name);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a small Windows Service which runs every 9 minutes and writes
I have a while loop in a windows service which runs for some x
I have started prgramming a windows service. I have added a notify icon from
that's what I have: a Windows Service C# multithreaded the service uses a Read-Write-Lock
I have developed a small WCF service which handles HTTP request. I want to
I have created a C# web service using visual studio to stop the windows
We have a Windows Azure Web Role on two extra-small instances that has been
I have a .net windows service that test network performance by downloading files from
We've built a windows service which uses some COM+ components (developed by us). It
Right now I have: a multithreaded windows service written in C++ which use common

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.