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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:34:25+00:00 2026-05-31T21:34:25+00:00

When TimerCallback is static, it will execute only once and just hang and will

  • 0

When TimerCallback is static, it will execute only once and just hang and will even block any other timers that are running such as timer2. It does reach the end of the method and no exceptions are being thrown. If I make the callback non-static then it works as expected calling the method every x seconds. If I remove the database context then it works fine static or not. The System.Threading.Timer should be queueing calls on a worker thread so even IF the db blocks a thread I don’t see how it can block ALL the threads, even the ones that are not accessing the database. Can anyone explain why it is behaving like this?

public class TimerClass
{
    private System.Threading.Timer timer1;
    private System.Threading.Timer timer2;

    public void Start(TimeSpan CheckInterval)
    {
        //timer2 = new Timer(o => Console.WriteLine("Timer2: " + DateTime.Now.ToString()), null, TimeSpan.Zero, TimeSpan.FromSeconds(1));
        timer1 = new Timer(TimerCallback, null, TimeSpan.Zero, CheckInterval);
    }

    private static void TimerCallback(object state)
    {
        Console.WriteLine("Timer1: " + DateTime.Now.ToString());
        using (MyDbEntities db = new MyDbEntities())
        {
            foreach (var item in db.Table)
                Console.WriteLine(item.Name);
        }

        Console.WriteLine("Leaving Callback...");
    }
}

This is where the Timer is started. After it is started this Main thread will continue fine but the Timers still hang.

static void Main()
{
    TimerClass myTimer = new TimerClass();
    myTimer.Start(TimeSpan.FromSeconds(1));
    Console.WriteLine("Timer started...");
    Console.ReadLine(); // pausing here, timers are still in-scope and should be firing
}
  • 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-31T21:34:27+00:00Added an answer on May 31, 2026 at 9:34 pm

    Then it calls ReadLine() so myTimer is never out of scope so how could they be garbage collected?

    No, that’s not how the garbage collector works. myTimer can be collected right after the Start() call since there are no further references to the object. If you want to keep myTimer alive until the end of the method then you’ll have to add GC.KeepAlive(myTimer) at the end the method.

    This is a specific problem with timers, particularly System.Threading.Timer. The garbage collector must see a live reference to an object to prevent it from getting garbage collected. No such reference exists in your code, the local variable doesn’t live long enough and the callback is static.

    The CLR does make some effort to keep a System.Timers.Timer going, it won’t be collected as long as it is Enabled. But as soon as it is disabled then it is eligible for collection. Which is fine of course, if there is no reference then you couldn’t enable it anymore. A System.Threading.Timer doesn’t have the same mechanism, you must keep it referenced yourself to keep it alive. Which is one of the core reasons that there are two Timer classes that seemingly do the same job. They don’t, System.Timers.Timer has cooties of its own.

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

Sidebar

Related Questions

i have a windows service that runs every 10 seconds to execute the read
I have been task with (ha) creating an application that will allow the users
I am trying to start a long running program that gives lots of output
I have a Windows Service application which uses a Threading.Timer and a TimerCallback to
I just asked this question . In short, when you throw from a win32
I've been tracking down a really insidious bug at work. The event that seems
I need to create a C++/CLI mixed assembly that can schedule future calls into
Running the following (slightly pseudo)code produces the following results. Im shocked at how innacurate
I use System.Threading.Timer to get accurate three minute intervals: private System.Threading.Timer ReadTimer; private System.Threading.TimerCallback
I wrote a VB.NET Windows Service, which works fine. I have only one issue

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.