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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:04:09+00:00 2026-06-04T11:04:09+00:00

Where to dispose StreamWriter if I need it during entire application lifetime? I’m going

  • 0

Where to dispose StreamWriter if I need it during entire application lifetime? I’m going to dispose it in destructor, will that work? I have to dispose to flush data, and I don’t want to use AutoFlush feature because from msdn: "You can get better performance by setting AutoFlush to false, assuming that you always call Close (or at least Flush) when you're done writing with a StreamWriter."

So should i Dispose in destructor like in the code below?

class Log
{
    private static StreamWriter swLog = new StreamWriter("logMAIN.txt");

    static ~Log()
    {
        swLog.Dispose();
    }

    public static void Push(LogItemType type, string message)
    {
        swLog.WriteLine(type + " " + DateTime.Now.TimeOfDay + " " + message);
    }
}

upd instead of Dispose i meant to call Close but it is not improtant in this case because they seems doing exactly the same.

  • 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-04T11:04:10+00:00Added an answer on June 4, 2026 at 11:04 am

    You seem to be basing your decision not to flush on some performance information from MSDN. That’s not where I’d start.

    Do you have evidence that using AutoFlush causes you significant performance problems?

    Have you considered alleviating these performance problems in a different way, e.g. having a single thread writing to the StreamWriter, either auto-flushing or periodically flushing every 20 seconds or whatever?

    You haven’t told us what kind of application you’re writing, mind you – that can make a significant difference in terms of how much you know about shutdown.

    Also note that the code you’ve given isn’t thread-safe to start with. You could end up using the StreamWriter from multiple threads concurrently; I doubt that StreamWriter is particularly designed for that scenario.

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

Sidebar

Related Questions

My root problem is that when using calls Dispose on a StreamWriter , it
The context I have an Asp .Net web application with some pages that inherit
I have a page that I need to call from another page more that
FileStream f=new FileStream(c:\\file.xml,FileMode.Create); StreamWriter sf=new StreamWriter(f); sf.WriteLine(stroka); sf.Close(); sf.Dispose(); f.Close(); f.Dispose(); FileStream f1=new FileStream(c:\\file.xml,FileMode.Open);
How to Dispose unused resources in c# winform application GLOBALLY ??
When i call object.Dispose() ; Will CLR immediately destroy the object from memory or
Why isn't HashAlgorithm.Dispose public? void IDisposable.Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } I understand that it
So I'm using a StreamReader that uses a MemoryStream to write to a StreamWriter
please help. I have this code, it's my class to serialize\deserialize application settings. [XmlRoot(EvaStartupData)]
Let's say I have the following code: StreamWriter sw = new StreamWriter(File.OpenWrite(Path)); sw.Write(Some stuff

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.