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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:18:48+00:00 2026-06-15T02:18:48+00:00

I am currently working on a project in C#. I am syncing access to

  • 0

I am currently working on a project in C#. I am syncing access to a state variable using a single lock. This state variable is triggered to be set for a given period of time and then should have its value reset. My current code is as follows.

using System.Threading;

class Test
{
  object syncObj = new object();
  bool state = false;
  Timer stateTimer;

  Test()
  {
    stateTimer = new Timer(ResetState, this, Timeout.Infinite, Timeout.Infinite);
  }

  void SetState()
  {
    lock(syncObj)
    {
      state = true;
      stateTimer.Change(1000, Timeout.Infinite);    
    }
  }

  static void ResetState(object o)
  {
    Test t = o as Test;
    lock(t.syncObj)
    {
      t.state = false;
    }
  }  
}

Given that it is valid to call SetState again before ResetState is called by the Timer (i.e. it is allowed to extend the period of time that state is true), I can imagine situations where a single lock may not be enough. The specific case I’m thinking of is this

  • Both SetState and ResetState are entered at the same time, on the main thread and the Timer thread respectively
  • SetState acquires the lock first and correctly sets state to true and triggers the timer to start again
  • ResetState then incorrectly sets state to false meaning that state is not true for the expected period of time

I’ve been scratching my head over this one for a little while. The closest I got to being able to solve it was by using two locks but in the end I found this caused other issues (at least, the way I’d done it).

Is there a known way to solve this problem (and should I be reading something to refresh my knowledge of synchronisation)?

UPDATE: I forgot to mention that the current state of the timer cannot be queried in this instance. If it could I would imagine checking the remaining time in ResetState to determine that the timer is really stopped.

  • 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-15T02:18:49+00:00Added an answer on June 15, 2026 at 2:18 am

    First and foremost: it’s a bad idea to expose the locking object publicly!

    class Test
    {
      private object syncObj = new object();
      private bool state = false;
      private Timer stateTimer;
    
      public Test()
      {
        stateTimer = new Timer(ResetState, this, Timeout.Infinite, Timeout.Infinite);
      }
    
      public void SetState()
      {
        lock(syncObj)
        {
          state = true;
          stateTimer.Change(1000, Timeout.Infinite);    
        }
      }
    
      public static void ResetState(object o)
      {
        Test t = o as Test;
        t.ResetState();
      }  
    

    Since you’re no longer exposing the locking object, you’ll have to create another method to reset the state:

      public void ResetState()
      {
        lock(syncObj)
        {
          state = false;
          stateTimer.Change(Timeout.Infinite, Timeout.Infinite);
        }
      }  
    
    
    }
    

    Note that we also take care of another problem in the new ResetState method and that is to force the timer not to fire again. This will only guarantee that the state flag will not be in out of sync with the timer; i.e. if you set the state, it will remain set for the expected amount of time or until the reset method is called.

    Update

    If you want to reject the reset attempt, then make the state variable an enum:

    enum EState
    {
        Off = 0,
        On = 1,
        Waiting = 2
    }
    
    private EState state = EState.Off;
    
    // Provide a state property to check if the state is on or of (waiting is considered to be Off)
    public bool State{ get{ return state == EState.On;} }
    

    In addition, you will now need to modify the SetState method and you will need two reset methods (the private one will be used with by the timer).

    public void SetState()
    {
        lock(syncObj)
        {
            state = EState.Waiting;
            stateTimer.Change(1000, Timeout.Infinite);
        }
    }
    
    public void ResetState()
    {
        lock(syncObj)
        {
            if(state != EState.Waiting)
            {
                state = EState.Off;
            }
        }
    }
    
    private void TimerResetState()
    {
        lock(syncObj)
        {
            state = EState.Off;
            stateTimer.Change(Timeout.Infinite, Timeout.Infinite);
        }
    }
    

    So now your constructor will look like this:

    public Test()
    {
        stateTimer = new Timer(TimerResetState, this, Timeout.Infinite, Timeout.Infinite);
    }
    

    Things should work roughly along those lines.

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

Sidebar

Related Questions

I am currently working on android project where I am using a custom list
I'm currently working on project euler problem 14 . I solved it using a
I am currently working on a project using Objectdb and Im using NetBeans. I
I am currently working on a project of mine using Prism (the Composite Application
I am currently working on a project where i am pulling out data using
I am currently working on a project which I think using soap as part
I'm currently working on a project using the Tkinter module in Python. I am
I'm currently working on a project using javascript and python with Jquery Datatables plugin
I'm currently working with a project using Hibernate + JPA. I don't recall exactly
I'm currently working on project, Android Internet Download Manager. In order to download the

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.