use a static StreamWriter?

Discussion in 'Microsoft Dot NET Framework Forms' started by gol, Nov 6, 2008.

  1. gol

    gol Guest

    Hi,
    I'm writing an application that needs every so often to write some data to a
    text file (some kind of a log file). I don't write a lot of data to the text
    file – just about 10 times, about 100 bytes each time, that's a total of
    approximately 1K, during a total period of approximately 2 minutes.

    My question is whether I should open a StreamWriter each time, this way I
    can write something like:

    using (StreamWriter sw…)
    {
    sw.WriteLine(…);
    }

    Or is it better to define some static StreamWriter, which I will not close
    until the end of the program. I hope I will always be able to detect an
    unexpected exit, and then close the StreamWriter, but I'm not sure about it.

    What I like in the first method is using the "using" keyword, which is
    impossible in the second.
    Can anyone tell what is better in my situation? What are the advantages and
    disadvantages of each method?
    Thanks a lot
     
    gol, Nov 6, 2008
    #1
    1. Advertisements

  2. gol

    Brian Gideon Guest

    On Nov 5, 11:46 pm, gol <> wrote:
    > Hi,
    > I'm writing an application that needs every so often to write some data to a
    > text file (some kind of a log file). I don't write a lot of data to the text
    > file – just about 10 times, about 100 bytes each time, that's a total of
    > approximately 1K, during a total period of approximately 2 minutes.
    >
    > My question is whether I should open a StreamWriter each time, this way I
    > can write something like:
    >
    > using (StreamWriter sw…)
    > {
    >      sw.WriteLine(…);
    >
    > }
    >
    > Or is it better to define some static StreamWriter, which I will not close
    > until the end of the program. I hope I will always be able to detect an
    > unexpected exit, and then close the StreamWriter, but I'm not sure about it.
    >
    > What I like in the first method is using the "using" keyword, which is
    > impossible in the second.
    > Can anyone tell what is better in my situation? What are the advantages and
    > disadvantages of each method?
    > Thanks a lot


    I'm not sure it's going to make a whole lot of difference since you're
    only talking about a few writes with small data amounts. But, I can
    you that I tend to hold resources open for the entire unit of work.
    Assuming the 2 minute figure you mentioned represents one logical unit
    of work then I would I recommend holding the StreamWriter open for the
    entire period. You can still use the 'using' keyword as long as you
    have the code structured accordingly.
     
    Brian Gideon, Nov 10, 2008
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Cavacas

    Static column in datagrid

    Paul Cavacas, Aug 25, 2003, in forum: Microsoft Dot NET Framework Forms
    Replies:
    0
    Views:
    223
    Paul Cavacas
    Aug 25, 2003
  2. Dave

    VS 2003 Debugger - static void Main()

    Dave, Sep 1, 2003, in forum: Microsoft Dot NET Framework Forms
    Replies:
    2
    Views:
    202
  3. Duray AKAR

    Adding handlers to events of a static variable in a class

    Duray AKAR, Sep 10, 2003, in forum: Microsoft Dot NET Framework Forms
    Replies:
    0
    Views:
    171
    Duray AKAR
    Sep 10, 2003
  4. Don Baechtel

    Change Font Size of Static Text

    Don Baechtel, Oct 14, 2003, in forum: Microsoft Dot NET Framework Forms
    Replies:
    3
    Views:
    1,247
    Joe White
    Oct 16, 2003
  5. Fred Mertz

    Static vs Non Static DAL Class(es)

    Fred Mertz, Dec 26, 2006, in forum: Microsoft Dot NET Framework Forms
    Replies:
    4
    Views:
    303
    Sir C4
    Dec 27, 2006
Loading...

Share This Page