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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:11:39+00:00 2026-06-11T17:11:39+00:00

Not really language specific, but I have this in C#: public static void StartJob()

  • 0

Not really language specific, but I have this in C#:

public static void StartJob() {
    ThreadPool.QueueUserWorkItem(s => {
        if (Monitor.TryEnter(_lock)) {
            ProcessRows();
            Monitor.Exit(_lock);
        }
    }
);

ProcessRows() processes and removes rows in a database until all rows are removed. At program launch and whenever a row is added to the database elsewhere in the program, StartJob is called, to ensure all rows are processed without blocking the program. Now if a row is added at exactly the same time as StartJob is about to release the lock, it will not be processed.

How do I ensure that all rows are processed? I prefer not to have ProcessRows() run unless rows are added.

  • 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-11T17:11:41+00:00Added an answer on June 11, 2026 at 5:11 pm

    Don’t lock around the method; lock within the method using a flag that says whether you’re running or not.

    One implementation might look like the code below – in this case I’ve moved all the logic into ProcessRows and made the method return immediately if it’s already running.

    public static bool _isRunning = false;
    
    public static void StartJob() 
    { 
        ThreadPool.QueueUserWorkItem(s => { ProcessRows(); })
    } 
    
    public static void ProcessRows()
    {
        Monitor.Enter(_lock);
        if (_isRunning) 
        {
            Monitor.Exit(_lock);
            return;
        }
    
        _isRunning = true;
    
        while (rowsToProcess)
        {
            Monitor.Exit(_lock);;
    
            // ... do your stuff
    
            Monitor.Enter(_lock);
        }
    
        _isRunning = false;
        Monitor.Exit(_lock);
    }
    

    With this structure, it’s impossible for the while loop to complete without setting _isRunning = false – if this weren’t the case, there would be a race condition if the loop completed just as another instance of the method started. Equally, when the method is called, it will always enter the loop and set _isRunning = true before another instance gets a chance to execute.

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

Sidebar

Related Questions

This is not really language-specific, but I'll use Python to explain my question. My
This does not really apply to any language specifically, but if it matters I
This is a question not really about programming (is not specific to any language
I'm not really sure how to ask this question, but I'll do my best.
This is really a python language question, but its wrapped around a Google appengine
I've seen a few questions that were similar but not specific enough. This one
Not really too sure how to word this question, therefore if you don't particularly
Not really sure how to phrase this other than by example.. Given... DATABASES =
I am not really sure how to word this question, so I did the
This is similar to this question , but kind of the opposite. I have

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.