473,508 Members | 2,265 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Scope of StreamWriter in Static Class; resource usage implications

In a 2.0 Windows Forms app... in a static class I have a method named
WriteToLocalLog() that writes to a text file on the local machine.

I declare a StreamWriter at the class level (not inside the method), as
follows:
private static System.IO.StreamWriter streamWriter_ToLocalLog;

The reason I declare streamWriter_ToLocalLog at the class level and not
inside the WriteToLocalLog() method is so I can reuse the StreamWriter
between calls to WriteToLocalLog().

My question:
Is there an obvious problem or risk with doing it this way? I'm wondering if
I would need to specifically .Close() it... else risk a memory leak or
otherwise tie up system resources once the app closes.

Guidance and perspective are appreciated.

Dec 6 '06 #1
1 1708


"Merk" <A@B.COMwrote in message
news:ur**************@TK2MSFTNGP04.phx.gbl...
In a 2.0 Windows Forms app... in a static class I have a method named
WriteToLocalLog() that writes to a text file on the local machine.

I declare a StreamWriter at the class level (not inside the method), as
follows:
private static System.IO.StreamWriter streamWriter_ToLocalLog;

The reason I declare streamWriter_ToLocalLog at the class level and not
inside the WriteToLocalLog() method is so I can reuse the StreamWriter
between calls to WriteToLocalLog().

My question:
Is there an obvious problem or risk with doing it this way? I'm wondering
if I would need to specifically .Close() it... else risk a memory leak or
otherwise tie up system resources once the app closes.

Guidance and perspective are appreciated.
The StreamWriter will be destroyed when your application closes, which is
apparently what you want. However, you should synchronize access to the
shared StreamWriter in case it's accessed from multiple threads.

David

Dec 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3136
by: pembed2003 | last post by:
Hi all, I am reading the book "C++ How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
4
14867
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
1
5496
by: Daniel | last post by:
If my System.IO.StreamWriter Write method throws "The specified network name is no longer available." and I try to Dispose or Close it in the finaly clause the close or dispose method just throws...
11
1898
by: Dave | last post by:
I'm trying to understand the implications of using static methods and properties in asp.net so I found an article "Troubleshooting ASP.NET applications with the use of static keywords"...
6
2481
by: GTi | last post by:
I have created a simple logger for my ASP.NET 2.0 application. I have this static function. My problem is that no data is created in the test.log file. I have added ASPNET user account...
12
2545
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
4
8409
by: Heron | last post by:
Hi, Could someone explain me why the following code doesn't work? The memorystream always remains with length 0. MemoryStream input = new MemoryStream();
4
2564
by: AndrewD | last post by:
Hey C++ folks, I created this today, just for fun. You can make object allocation for any class around 6 times faster, simply by doing the following. class MyClass : public...
0
35189
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7401
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7063
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.