Solved

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

Posted on 2006-11-29
3
2,174 Views
Last Modified: 2008-01-09
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?
0
Comment
Question by:CBeach1980
  • 2
3 Comments
 
LVL 7

Expert Comment

by:Dimandja
Comment Utility
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.
0
 
LVL 4

Author Comment

by:CBeach1980
Comment Utility
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.
0
 
LVL 7

Accepted Solution

by:
Dimandja earned 500 total points
Comment Utility
You will have only one thread call to the datareader at a time.  Therefore, you really don't gain anything by using multiple threads, because the threads will be waiting on each other.

Depending on your database organization, you should be able to issue queries that use INDEXES.  This limits how much data is accessed per query.  This also could make it possible to issue queries that return limited and separate data sets.  For example, querying for rows where the index starts with the letter "A" will only return those records, while the next thread may want to return the "B"s, and so on.

Building indexes insures that your whole database is not searched, if you base your query on the index.  It's all about planning.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 32
Why, and when, to use Windows Workflow 1 15
Achieve json result 2 20
XML & .net 5 14
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now