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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:15:02+00:00 2026-06-02T23:15:02+00:00

I had a multi-threaded TCP server software written in C#. Many of the threads

  • 0

I had a multi-threaded TCP server software written in C#. Many of the threads should read from database and i am using SqlDataReader for reading from database. When one thread wants to read from database, there is no problem but when two or more threads want to read from database, this situation starts to be a problem. .NET gives this exception: There is already an open DataReader associated with this Command which must be closed first.

How can i prevent from the error? How can i protect SqlDataReader from multiple access? Is mutex appropriate for this problem?

  • 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-02T23:15:04+00:00Added an answer on June 2, 2026 at 11:15 pm

    I suggest creating a new connection per request instead of trying to re-use an existing active connection. I assume you are using some sort of class member or static reference to the connection, instead make it method scoped:

    private void RespondingToTcpRequest()
    {
        using (var connection = new SqlConnection(""))
        using (var command = new SqlCommand("", connection))
        {
             connection.Open();
    
             using (var reader = command.ExecuteReader())
             {
                 // No locks or sharing issues here!
             }
        }
    }
    

    If I had some sample code demonstrating what you currently have, I could adapt that idea for your situation.

    As for locking, if you really want to go down this route (seriously, don’t), all you need to do is lock (one of the basic synchronisation primitives):

    private static readonly object ReaderLock = new object();
    
    private void RespondingToTcpRequest()
    {
        lock (ReaderLock)
        {
            // database connection, command, reader stuff here.
        }
    }
    

    A mutex won’t necessarily be required as the problem will be process-local, where the lock keyword will suffice. Again, seriously, don’t.

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

Sidebar

Related Questions

I'm using Java 6. I've only written a couple of multi-threaded applications so I've
Is I am looking at writing a multithreaded tcp server using boost ASIO. I
I am writing a multi-threaded .Net application in which many callers need to concurrently
I'm creating a multi-tenant ASP.NET application using NHibernate (shared schema). I had intended to
We are using a software library from a major US manufacturer. For years the
I'm working on some big multi threaded project, now yesterday I had a deadlock
I had a multi-thread program which execute from dos prompt, there are some prints
We had multi-threaded application. Each thread has to invoke with CoIntialize when it is
I am working on a school project where I had to write a multi-threaded
I've had a working version of multi-threaded code, however I was unsatisfied with my

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.