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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:41:03+00:00 2026-06-08T20:41:03+00:00

I have a few different methods which connect to a remote host, send a

  • 0

I have a few different methods which connect to a remote host, send a message, get a reply, and use the information. this has worked fine until i use two methods in the same instance of my connection class. in the example where i’m getting the error, i run the following method;

public string sendRequestAccountID(string siteID)
{
    //build message
    String response = String.Empty;

    TcpClient client = new TcpClient();
    client.Connect(detailsHere);
    NetworkStream stream = client.GetStream();

    StreamWriter writer = new StreamWriter(stream);
    writer.AutoFlush = false;

    writer.WriteLine(sb.ToString());
    writer.Flush();

    StreamReader reader = new StreamReader(stream);
    List<XmlNode> nodeList = new List<XmlNode>();

    byte[] responseBytes = new byte[4096];
    int bytesRead = 0;

    while (true)
    {
        bytesRead = stream.Read(responseBytes, 0, responseBytes.Length);
        if (bytesRead > 0)
        {
            //handle message
        }
        if (bytesRead == 0)
        {
            stream.Flush();
            stream.Close();
            client.Close();
            string finalResponse = stuffHereToSend;
            return finalResponse;
        }
    }
}

This sends fine, and returns a message as expected. However, if i then use the same instance of my connection class and use the following method;

public bool sendNewDevice(IDeviceInterface device)
{
    NetworkStream stream;
    sb = new StringBuilder();
    //build message
    String response = String.Empty;

    TcpClient client = new TcpClient();
    client.Connect(detailsHere);
    stream = client.GetStream();

    StreamWriter writer = new StreamWriter(stream);
    writer.AutoFlush = false;

    writer.WriteLine(sb.ToString());
    writer.Flush();

    StreamReader reader = new StreamReader(stream);
    List<XmlNode> nodeList = new List<XmlNode>();

    byte[] responseBytes = new byte[4096];
    int bytesRead = 0;

    while (true)
    {
        bytesRead = stream.Read(responseBytes, 0, responseBytes.Length);
        if (bytesRead > 0)
        { 
            //handle message
        }
    }
}

I’m getting an error which reads “cannot access a disposed object” on;

 bytesRead = stream.Read(responseBytes, 0, responseBytes.Length);

Although i thought that i’d newly just assigned the stream in the latest method. Is it trying to use the previously closed one? is there a way around this or something silly that i’m missing?

edit: is it something to with the clients not disposing correctly? the two methods are ran within a second of each other, maybe the second is trying to open before the first has closed?

  • 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-08T20:41:04+00:00Added an answer on June 8, 2026 at 8:41 pm

    When a StreamWriter (and reader) is closed or its Dispose method is called, it disposes the underlying stream. Prior to .net 4.5 there really wasn’t anything you could do about it, other than use something other than StreamWriter or write a class to wrap the Stream given to StreamWriter and ignore the call to Dispose. in .NET 4.5, there is an overload you can use to tell the StreamWriter not to dispose the stream you give it. eg: new StreamWriter(stream, StreamWriter.UTF8NoBOM, 1024, false)

    You could try using a wrapped stream to ignore the close (calling new StreamWriter(new NonDisposableStreamWrapper(stream))):

    public class NonDisposableStreamWrapper : Stream
    {
        private Stream wrappedStream;
    
        public NonDisposableStreamWrapper(Stream wrappedStream)
        {
            this.wrappedStream = wrappedStream;
        }
    
        public override void Flush()
        {
            wrappedStream.Flush();
        }
    
        public override long Seek(long offset, SeekOrigin origin)
        {
            return wrappedStream.Seek(offset, origin);
        }
    
        public override void SetLength(long value)
        {
            wrappedStream.SetLength(value);
        }
    
        public override int Read(byte[] buffer, int offset, int count)
        {
            return wrappedStream.Read(buffer, offset, count);
        }
    
        public override void Write(byte[] buffer, int offset, int count)
        {
            wrappedStream.Write(buffer, offset, count);
        }
    
        public override bool CanRead
        {
            get { return wrappedStream.CanRead; }
        }
    
        public override bool CanSeek
        {
            get { return wrappedStream.CanSeek; }
        }
    
        public override bool CanWrite
        {
            get { return wrappedStream.CanWrite; }
        }
    
        public override long Length
        {
            get { return wrappedStream.Length; }
        }
    
        public override long Position
        {
            get { return wrappedStream.Position; }
            set { wrappedStream.Position = value; }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few different classes which origin is a another class. I have
Background : I have a few different threads which each need to write to
I have tried this a few different ways and it always seems to fail.
I have been researching a few different apps lately for my company which are
I have a method in a class for which they are a few different
I have an interface A, for which I have to supply a few different
I am writing an application, where I do have few different windows implemented, where
I have a few different applications that i need to share code between to
I have a few different things open in the terminal whenever I'm developing --
We have a few different programs all compiled together in the same suite, recently

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.