473,586 Members | 2,863 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.Strea mWriter streamWriter_To LocalLog;

The reason I declare streamWriter_To LocalLog 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 1715


"Merk" <A@B.COMwrote in message
news:ur******** ******@TK2MSFTN GP04.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.Strea mWriter streamWriter_To LocalLog;

The reason I declare streamWriter_To LocalLog 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
3146
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 function-prototype scope I think(might be wrong):
4
14883
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 strongly name which contains the class where the static variable is defined. This library can be referenced by multiple projects. I am fairly sure the...
1
5503
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 "The specified network name is no longer available." again. how to clean this up? after this the stream writer is stuck connected until i restart my...
11
1907
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" (http://support.microsoft.com/?id=893666) that discusses the possiblity of users seeing other users data if they access static methods at the same time because...
6
2487
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 administrator rights. What am I missing here? namespace MyNameSpace { public class LOG {
12
2561
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 advance, Joe
4
8418
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
2569
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 TXpQAlloc<MyClass,N> N is a chunking factor (defaults to 10).
0
35199
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 the sake of brevity I am sticking to common usage. Wherever the term procedure is used in this tutorial it actually refers to a subroutine or...
0
7911
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8215
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3836
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.