Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Error : Cannot write to a closed TextWriter

Error : Cannot write to a closed TextWriter

Scheduled Pinned Locked Moved C#
helptutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adityap
    wrote on last edited by
    #1

    Hi, I am getting the above mentioned error while excuting the following simple example ( relating to dispose and finalize). Exact output is also attached after the program. Can any body explain me the cause for this? Thanks a lot in advance //#PROGRAM using System; namespace test { //Base.cs public class Base : IDisposable { private bool disposed = false; private readonly int id; public int ID { get { return id; } } public Base(int theID) { id = theID; } public void Disp() { Console.WriteLine("You can run me after disposing{0}",id); } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { Console.WriteLine("Base Cleaning up managed resources on {0}",id); // Code to clean up managed resources } Console.WriteLine("Base Cleaning up unmanaged resources on {0}", id); // Code to clean up unmanaged resources } disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } ~Base() { //Following line gives error for object 2 Console.WriteLine("*** Finalize called on Base {0}", id); Dispose(false); } } //Derived.cs public class Derived : test.Base { private bool disposed = false; public Derived(int theID) : base(theID) {} protected override void Dispose(bool disposing) { if (!disposed) { try { if (disposing) { Console.WriteLine("Derived Cleaning up managed resources"); // Code to clean up managed resources } Console.WriteLine("Derived Cleaning up unmanaged resources"); // Code to clean up unmanaged resources } finally { base.Dispose(disposing); } } disposed = true; } } //Test.cs class Test { [STAThread] static void Main(string[] args) { Derived object1 = new Derived(1); Derived object2 = new Derived(2); object1.Dispose(); } } } //#OUTPUT Derived Cleaning up managed resources Derived Cleaning up unmanaged resources Base Cleaning up managed resources on 1 Base Cleaning up unmanaged resources on 1 An unhandled exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll Additional information: Cannot write to a closed TextWriter. Unhandled Exception: System.ObjectDisposedException: Cannot write to a closed TextWriter. at System.IO.__Error.WriterClosed() at System.IO.

    J 2 Replies Last reply
    0
    • A adityap

      Hi, I am getting the above mentioned error while excuting the following simple example ( relating to dispose and finalize). Exact output is also attached after the program. Can any body explain me the cause for this? Thanks a lot in advance //#PROGRAM using System; namespace test { //Base.cs public class Base : IDisposable { private bool disposed = false; private readonly int id; public int ID { get { return id; } } public Base(int theID) { id = theID; } public void Disp() { Console.WriteLine("You can run me after disposing{0}",id); } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { Console.WriteLine("Base Cleaning up managed resources on {0}",id); // Code to clean up managed resources } Console.WriteLine("Base Cleaning up unmanaged resources on {0}", id); // Code to clean up unmanaged resources } disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } ~Base() { //Following line gives error for object 2 Console.WriteLine("*** Finalize called on Base {0}", id); Dispose(false); } } //Derived.cs public class Derived : test.Base { private bool disposed = false; public Derived(int theID) : base(theID) {} protected override void Dispose(bool disposing) { if (!disposed) { try { if (disposing) { Console.WriteLine("Derived Cleaning up managed resources"); // Code to clean up managed resources } Console.WriteLine("Derived Cleaning up unmanaged resources"); // Code to clean up unmanaged resources } finally { base.Dispose(disposing); } } disposed = true; } } //Test.cs class Test { [STAThread] static void Main(string[] args) { Derived object1 = new Derived(1); Derived object2 = new Derived(2); object1.Dispose(); } } } //#OUTPUT Derived Cleaning up managed resources Derived Cleaning up unmanaged resources Base Cleaning up managed resources on 1 Base Cleaning up unmanaged resources on 1 An unhandled exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll Additional information: Cannot write to a closed TextWriter. Unhandled Exception: System.ObjectDisposedException: Cannot write to a closed TextWriter. at System.IO.__Error.WriterClosed() at System.IO.

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      Is the console being routed somewhere else? Writing to the console is causing an issue, an ObjectDisposedException, as if the console itself was disposed. Is the application exiting or something when you're writing to the console? If this is a console application, try adding a Console.ReadLine() at the end of your Main method. That way it won't exit until you press a key.

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

      1 Reply Last reply
      0
      • A adityap

        Hi, I am getting the above mentioned error while excuting the following simple example ( relating to dispose and finalize). Exact output is also attached after the program. Can any body explain me the cause for this? Thanks a lot in advance //#PROGRAM using System; namespace test { //Base.cs public class Base : IDisposable { private bool disposed = false; private readonly int id; public int ID { get { return id; } } public Base(int theID) { id = theID; } public void Disp() { Console.WriteLine("You can run me after disposing{0}",id); } protected virtual void Dispose(bool disposing) { if (!disposed) { if (disposing) { Console.WriteLine("Base Cleaning up managed resources on {0}",id); // Code to clean up managed resources } Console.WriteLine("Base Cleaning up unmanaged resources on {0}", id); // Code to clean up unmanaged resources } disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } ~Base() { //Following line gives error for object 2 Console.WriteLine("*** Finalize called on Base {0}", id); Dispose(false); } } //Derived.cs public class Derived : test.Base { private bool disposed = false; public Derived(int theID) : base(theID) {} protected override void Dispose(bool disposing) { if (!disposed) { try { if (disposing) { Console.WriteLine("Derived Cleaning up managed resources"); // Code to clean up managed resources } Console.WriteLine("Derived Cleaning up unmanaged resources"); // Code to clean up unmanaged resources } finally { base.Dispose(disposing); } } disposed = true; } } //Test.cs class Test { [STAThread] static void Main(string[] args) { Derived object1 = new Derived(1); Derived object2 = new Derived(2); object1.Dispose(); } } } //#OUTPUT Derived Cleaning up managed resources Derived Cleaning up unmanaged resources Base Cleaning up managed resources on 1 Base Cleaning up unmanaged resources on 1 An unhandled exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll Additional information: Cannot write to a closed TextWriter. Unhandled Exception: System.ObjectDisposedException: Cannot write to a closed TextWriter. at System.IO.__Error.WriterClosed() at System.IO.

        J Offline
        J Offline
        Judah Gabriel Himango
        wrote on last edited by
        #3

        p.s. Next time surround your code with <pre> tags.

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

        A 1 Reply Last reply
        0
        • J Judah Gabriel Himango

          p.s. Next time surround your code with <pre> tags.

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

          A Offline
          A Offline
          adityap
          wrote on last edited by
          #4

          I have tried putting Console.ReadLine(), but the error remains the same :sigh: And yes i am sorry for that ill-formatted code.. i'll remember to put

          next time..

          -- modified at 0:46 Tuesday 11th July, 2006

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups