Link to home
Start Free TrialLog in
Avatar of CBeach1980
CBeach1980

asked on

Multi-thread access to a single datareader in C#.net 2.0

I am trying to figure out a way to access a single datareader object from multiple threads.  I create the datareader and pass it into multiple instances of a thread that read from the datareader.  The goal is to have two or more threads reading from the datareader so that thread 1 reads half the records and thread 2 reads the other half.  I can get the threads to not throw errors by messing with the isolation type of the transaction for the connection but only one threads seems to read anything from the datareader and even then it reads only a fraction of the records.  Any ideas on how to accomplish this?
Avatar of Dimandja
Dimandja
Flag of United States of America image

To successfully point multiple threads to a STATIC datareader, you must make the datareader thread safe by serializing calls to it.  This in fact defeats the purpose of having multiple threads.

The best thing to do is have each thread create its own datareader.  Each thread can issue queries that it can handle exclusively.
Avatar of CBeach1980
CBeach1980

ASKER

The problem with each creating its own datareader is that I have an enormous dataset (>13 million rows) and would rather not parse the data into smaller sets simply because selecting a subset will take longer because it has to query the data.  If you serialize calls to the datareader how exactly does that impact the datareader and the threads?  I'm not really familiar with serializing calls.
ASKER CERTIFIED SOLUTION
Avatar of Dimandja
Dimandja
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial