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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:41:33+00:00 2026-05-20T18:41:33+00:00

I stumbled across some code similar to below: private void SomeCallBack(object state) { lock

  • 0

I stumbled across some code similar to below:

private void SomeCallBack(object state)
    {
        lock (_lock)
        {
            try
            {
                if (_timer == null)
                    return;

                _timer.Dispose();
                // do some work here
            }
            catch
            {
                // handle exception
            }
            finally
            {
                _timer = new Timer(SomeCallBack, state, 100, Timeout.Infinite);
            }
        }
    }

I don’t understand the purpose of recreating the timer every time the callback is executed. I think what the code is trying to achieve is that only one thread can perform the work at a time. But wouldn’t the lock be sufficient?

Also, according to msdn,

Note that callbacks can occur after the Dispose() method overload has been called

Is there any benefits of doing this?
If so, do the benefits justify the overheads in disposing and creating the timer?

Thanks for your help.

  • 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-20T18:41:33+00:00Added an answer on May 20, 2026 at 6:41 pm

    It seems that the code wants a nearly periodic timer (not exactly periodic because of the jitter introduced by the processing between the expiration of the timer and creation of the new timer). Disposing and recreating the timer is indeed an unnecessary overhead. The Change method would be better.

    The check for null is also curious; somewhere else there would have to be code that sets _timer null for it to have any effect.

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

Sidebar

Related Questions

I stumbled across some code and I was curious as to possible reasons why
I have stumbled across a bit of code that I was hoping someone could
I'm trying to remove some logging dependencies and stumbled across Castle Windsor's logging facility.
I recently stumbled across the Javadoc for the Collection.checkedMap family of functions for creating
NHydrate is a lesser-known ORM for .Net - I stumbled across it for the
I'm, let's say, new to jquery. I've stumbled across a quite interesting problem. I`ve
I'm aware some of you may not be familiar with Monotouch, but this could
In my code, I have a class that maintains a number of lists. We'll
I'm currently porting a Windows C++ library to MacOS as a hobby project as
For a website i am developing in django i need users to be able

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.