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 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 just stumbled across this bug in some legacy code: class MyAPIHandler { private:
Googling is only coming up with the keyword, but I stumbled across some code
I'm familiar with $get() and $find() . But, I've just stumbled across some code
I have stumbled across some code that is adding strings to a List but
I'm doing some refactoring work on PHP/MySQL code and stumbled across an interesting problem,
I stumbled across some code and I was curious as to possible reasons why
I have some code that parses through an html file and I stumbled across
I stumbled across this code and am too proud to go and ask the
I have just stumbled across the Backgroundworker object, and it seems like the tool
Stumbled across the ∴ operator in some of the rvm install scripts and couldn't

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.