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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:27:03+00:00 2026-05-24T23:27:03+00:00

I spin off a new thread that reads data from a database. Every X

  • 0

I spin off a new thread that reads data from a database. Every X records a flag is signaled where the main thread will then process the records that have been retrieved keeping some and discarding others. When the flag is signaled I use a lock to allow the reader thread to wait until the processor thread to finish processing the records. However, it appears the lock is not doing this because as I iterate through the records, more keep getting added (indicating the reader thread is still reading). This causes the collection to be modified resulting in an InvalidOperationExecption.

Maybe I have misunderstood what the purpose of “lock” is or I am not using it correctly. Here is pseudo of what I have:

readonly object locker = new object();
Dictionary screened = new Dictionary;

Search(){
    Thread reader = new Thread( () => Read("search terms") );
    reader.Start();

    while( found < desiredAmount ){
        if(SIGNAL){
            lock(locker){
                ProcessRecords();
            }
        }
     }
}

Read(){
    Connect to DB
    while(reader.Read()){
        screened.add(record);
    }
}

ProcessRecords(){
    foreach(var x in screened){
        //process record 
    }
}

I hope the pseudo was good enough, from my understanding Read() should not execute while in the lock block. Please help me understand lock a little better.

PS Yes I have read MSDNs articles on locks and still do quite grasp how to use lock in more complex situations.

  • 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-24T23:27:04+00:00Added an answer on May 24, 2026 at 11:27 pm

    You’ll need to put a lock around the while loop as well. The lock would work if you have two or more threads contending for the same lock, in your above sample, you have no contention, because no other thread requests a lock apart from the first.

    Read(){
        Connect to DB
            while(reader.Read()){
                lock(locker)
                    screened.add(record);
            }
    
    }
    

    A better way would be to put the lock inside ProcessRecords() as well.

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

Sidebar

Related Questions

I need to find a way to spin off a thread from a static
When the user saves some data, I want to spin off a background thread
I have my development database, I want to spin off a duplicate for testing.
I have a WCF service method that's running in a worker thread I spin
Note : This is a spin-off from Comparable and Comparator contract with regards to
This is a spin off from another question I posted a few days ago
This is a spin off from my last question How does delphi convert ModalResult
This question is a spin-off/evolution of this question . (That question got marked as
Is there a consistent way across browsers to hide the new spin boxes that
what would happen if I spin off a thread to execute a long-running process

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.