PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 5.00 average.

use a static StreamWriter?

 
 
gol
Guest
Posts: n/a
 
      6th Nov 2008
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

 
Reply With Quote
 
 
 
 
Brian Gideon
Guest
Posts: n/a
 
      10th Nov 2008
On Nov 5, 11:46*pm, gol <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Where to use static constructor and static class Ashok kumar Microsoft C# .NET 1 27th May 2010 04:17 PM
To use static or to not use static? Brett Romero Microsoft C# .NET 17 8th Dec 2005 10:22 PM
Static interfaces? Static method attributes? rogercnorris@yahoo.com Microsoft Dot NET Framework 1 1st Feb 2005 06:34 PM
Static data access method vs non-static method CW Microsoft ASP .NET 1 31st Mar 2004 06:34 PM
How static is static? - please help Tilfried Weissenberger Microsoft Dot NET Framework 2 20th Jul 2003 08:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:40 AM.