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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:33:17+00:00 2026-05-25T20:33:17+00:00

I have a scenario where my C# class has two methods say DoThis() and

  • 0

I have a scenario where my C# class has two methods say DoThis() and DoThat() that are called independent of each other, in any order, by an external caller. The two methods need to be synchronized in the following way:

  • After a call to DoThis(), wait at least t1 seconds before proceeding with DoThat() execution
  • After a call to DoThat(), wait at least t2 seconds before proceeding with DoThis() execution

So essentially in pseudocode:

static SomeCustomTimer Ta, Tb;
static TimeSpan t1, t2;

public static void DoThis()
{
    if(Tb.IsRunning())
        Tb.WaitForExpiry();

    DoStuff();
    Ta.Start(t1);
}

public static void DoThat()
{
    if(Ta.IsRunning())
        Ta.WaitForExpiry();

    DoOtherStuff();
    Tb.Start(t2);
}

DoStuff() and DoOtherStuff() are not long-running methods and do not share resources otherwise. Typically DoThis() and DoThat() will not be called concurrently. But I still need to protect against potential deadlocks.

How can I best implement DoThis(), DoThat() in C#?

EDIT
My scenario right now is simple in that there aren’t an arbitrary number of threads calling these functions. For purpose of simplification, there’s a single caller thread calling these functions in an arbitrary sequence. So the two methods will not be called concurrently, instead the caller will call these methods one-by-one in any order. I don’t have control over the caller thread’s code so I want to enforce the delay between successive calls to DoThis(), DoThat().

  • 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-25T20:33:18+00:00Added an answer on May 25, 2026 at 8:33 pm

    This is pretty easy to solve with a timed latch. A latch is synchronization mechanism that is either opened or closed. When open threads are allowed to pass through. When closed threads cannot get through. A timed latch is one that will automatically reopen or reclose after a certain amount of time has elapsed. In this case we want a “normally opened” latch so the behavior is biased towards staying open. That means the latch will reopen automatically after the timeout, but close only if Close is explicitly called. Multiple calls to Close will reset the timer.

    static NormallyOpenTimedLatch LatchThis = new NormallyOpenTimedLatch(t2);
    static NormallyOpenTimedLatch LatchThat = new NormallyOpenTimedLatch(t1);
    
    static void DoThis()
    {
      LatchThis.Wait();  // Wait for it open.
    
      DoThisStuff();
    
      LatchThat.Close();
    }
    
    static void DoThat()
    {
      LatchThat.Wait(); // Wait for it open.
    
      DoThatStuff();
    
      LatchThis.Close();
    }
    

    And we can implement our timed latch like the following.

    public class NormallyOpenTimedLatch
    {
        private TimeSpan m_Timeout;
        private bool m_Open = true;
        private object m_LockObject = new object();
        private DateTime m_TimeOfLastClose = DateTime.MinValue;
    
        public NormallyOpenTimedLatch(TimeSpan timeout)
        {
            m_Timeout = timeout;
        }
    
        public void Wait()
        {
            lock (m_LockObject)
            {
                while (!m_Open)
                {
                    Monitor.Wait(m_LockObject);
                }
            }
        }
    
        public void Open()
        {
            lock (m_LockObject)
            {
                m_Open = true;
                Monitor.PulseAll(m_LockObject);
            }
        }
    
        public void Close()
        {
            lock (m_LockObject)
            {
                m_TimeOfLastClose = DateTime.UtcNow;
                if (m_Open)
                {
                    new Timer(OnTimerCallback, null, (long)m_Timeout.TotalMilliseconds, Timeout.Infinite);
                }
                m_Open = false;
            }
        }
    
        private void OnTimerCallback(object state)
        {
            lock (m_LockObject)
            {
                TimeSpan span = DateTime.UtcNow - m_TimeOfLastClose;
                if (span > m_Timeout)
                {
                    Open();
                }
                else
                {
                    TimeSpan interval = m_Timeout - span;
                    new Timer(OnTimerCallback, null, (long)interval.TotalMilliseconds, Timeout.Infinite);
                }
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Scenario I have a class library that is going to be called from
I have a scenario in which I have a class Resource which has two
I have a very simple scenario, using NHibernate: one abstract base class animal; two
Here's the scenario: I'd like to have a host class that can have a
Hypothetical scenario: I have two models: Author and Book . The Book model has
PHP's DateTime class has the two methods add() and sub() to add or subtract
Scenario I have the following two overloads that I need in order to instantiate
I have a handful of controllers and each controller has a test class with
Consider this scenario we have a class A which is singleton it has a
I have a similar scenario as this one: public class TestLinq2Xml { private XElement

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.