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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:17:34+00:00 2026-06-12T08:17:34+00:00

I have a method (let’s call it CheckAll) that is called from multiple areas

  • 0

I have a method (let’s call it “CheckAll”) that is called from multiple areas of my program, and can therefore be called for a 2nd time before the 1st time has completed.

To get around this I have implemented a “lock” that (if I understand it correctly), halts the 2nd thread until the 1st thread has completed.

However what I really want is for this 2nd call to return to the calling method immediately (rather than halt the thread), and to schedule CheckAll to be run again once it has completed the 1st time.

I could setup a timer to do this but that seems cumbersome and difficult. Is there a better way?

  • 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-12T08:17:36+00:00Added an answer on June 12, 2026 at 8:17 am

    Easy/cheap implementation.

    private Thread checkThread = null;
    private int requests = 0;
    
    void CheckAll()
    {
     lock(SyncRoot){
        if (checkThread != null; && checkThread.ThreadState == ThreadState.Running)
        {  
            requests++;
            return;
        }else
        {
            CheckAllImpl();
        }
     }
    
    }
    
    void CheckAppImpl()
    {
     // start a new thread and run the following code in it.
      checkThread  = new Thread(newThreadStart( () => {
     while (true)
     {
     
     // 1. Do what ever checkall need to do.
     // 2.
         lock (SyncRoot)
         {
             requests--;
             if  (!(requests > 0))
                break;
         }
     }});
     checkThread.Start();
    }
    

    Just on a side note, this can have some race conditions. Better implementation would be to use ConcurrentQueue introduced in .NET 4 which handles all the threading craziness for you.

    Update

    Here’s a more ‘cool’ implementation using ConcurrentQueue (turns out we don’t need TPL):

    public class CheckAllService
    {
        // Make sure you don't create multiple 
        // instances of this class. Make it a singleton.
    
        // Holds all the pending requests
        private ConcurrentQueue<object> requests = new ConcurrentQueue<object>();
    
        private object syncLock = new object();
    
        private Thread checkAllThread;
    
        /// <summary>
        /// Requests to Check All. This request is async, 
        /// and will be serviced when all pending requests 
        /// are serviced (if any).
        /// </summary>
        public void RequestCheckAll()
        {
            requests.Enqueue("Process this Scotty...");
    
            lock (syncLock)
            {   // Lock is to make sure we don't create multiple threads.
                if (checkAllThread == null || 
                    checkAllThread.ThreadState != ThreadState.Running)
                {
                    checkAllThread = new Thread(new ThreadStart(ListenAndProcessRequests));
                    checkAllThread.Start();
                }
            }
        }
    
        private void ListenAndProcessRequests()
        {
            while (requests.Count != 0)
            {
                object thisRequestData;
                requests.TryDequeue(out thisRequestData);
                try
                {
                    CheckAllImpl();
                }
                catch (Exception ex)
                {
                    // TODO: Log error ?
                    // Can't afford to fail.
                    // Failing the thread will cause all 
                    // waiting requests to delay until another 
                    // request come in.
                }
            }
        }
    
        protected void CheckAllImpl()
        {
            throw new NotImplementedException("Check all is not gonna write it-self...");
            // TODO: Check All
        }
    }
    

    Note

    I use a real Thread instead of a TPL Task because a Task doesn’t hold on to a real thread as an optimization. When there’s no Thread, that means at the time your application closes, any waiting CheckAll requests are ignored. (I got bitten hard by this when I thought I’m so smart to call my logging methods in a task once, which ignored a couple of dozen log records when closing. CLR checks and waits for any waiting threads when gracefully exiting).

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

Sidebar

Related Questions

Let's say I have a subroutine/method that a user can call to test some
Let's say you have a business logic method that can perform some operation across
I have a method (GetDataReader, let's call it) that returns a SqlDataReader. It's inside
I have a method (let's call it run): - (void)run { // Do some
Let's say that I have a method that looks like this: def raise_if_client_status_error(xml_resp) #
Let's say I have a structure named vertex with a method that adds two
Let say I have a method (example taken from another post): public IQueryable<CityBlock> GetCityBlocks(){
Let's suppose I have some method that returns a IEnumerable<int> object. This methods make
Let's say I have a method m() that takes an array of Strings as
Let's say you have a method that expects a numerical value as an argument.

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.