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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:14:39+00:00 2026-05-15T04:14:39+00:00

being new to c# I’ve run into this ‘conundrum’ when passing around a SqlDataReader

  • 0

being new to c# I’ve run into this ‘conundrum’ when passing around a SqlDataReader between different threads. Without going into too much detail, the idea is to have a main thread fetching data from the database (a large recordset) and then have a helper-task run through this record by record and doing some stuff based upon the contents of this. There is no feedback to the recordset, it simply wades through until no records are left.
This works fine, but given the nature of the job at hand it should be possible to have this job spread over different threads (CPUs) to maximize throughput (the order of execution is of no significance). The question then becomes, when I pass this recordset in a SqlDataReader, do I have to use ref or not ? It kind of boils down to the question : if I pass the object around without specifying ref, won’t it create new copies in memory and have records processed n times ? Or, don’t I risk having the record-position being moved forward while not all fields have been fully read yet ? The latter seems more like a ‘data racing’ issue and probably is covered by the lock()ing mechanism (or not?).

My initial take on the problem was that it doesn’t really hurt passing the variable using ref, yet as a colleague put it : “you only need ref when you’re doing something wrong” =)
Additionally using ref restricts me from applying a Using() construction too which isn’t very nice either.
I thus create a “basic” project that tackles the same approach but without the ref notation. Tests so far show that it works flawlessly on a Core2Duo (2cpu) using any number of threads, yet I’m still a bit wary…
What do you experts think about this ? Use ref or not ?

You can find the test-project here as it seems I can’t upload it to this question directly ?!?

ps: it’s just a test-project and I’m new to c#, so please be gentle on me when breaking down the code =P

  • 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-15T04:14:40+00:00Added an answer on May 15, 2026 at 4:14 am

    Don’t pass SqlDataReader around to multiple threads. The documentation says this class is not thread safe.

    Even if it were, you would have all sorts of timing issues arising from this, especially when it comes time to dispose of your SqlDataReader. Add to that the fact that SqlDataReader already streams data in on another thread, and I think you’re prematurely optimizing.

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

Sidebar

Related Questions

Being new to test based development, this question has been bugging me. How much
being new to WPF this is a complex problem for me. What i want
I figured this would be simple, but being new to SSRS I'm not sure.
Being new to JQuery and trying to understand all the posts about different ways
Being new to this I really am trying to learn how to keep code
Being new to using threads etc in UI's, can I ask is the following
Being new to tablet programming this occured to me. I could see that there
Being new to working with Data, I hope I'm asking this properly. How can
Being new to iPhone development I am really struggling with this concept. I have
I have done this many times in PHP using JS but being new to

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.