473,698 Members | 2,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Call Close in Using Block with StreamWriter

I'm coding in VB.NET 2005 with a Using block and a StreamWriter as in

Using strWrite As New IO.StreamWriter (fileName, True)
strWrite.WriteL ine(Now & vbTab & cLineToWrite)
End Using

Should I explicitly call the Close method on the StreamWriter or is the
object closed and disposed when the Using block is exited?

Thanks for your insights.
Apr 7 '08 #1
5 2569
On Apr 7, 10:53 pm, JohnMSyrasoft
<JohnMSyras...@ discussions.mic rosoft.comwrote :
I'm coding in VB.NET 2005 with a Using block and a StreamWriter as in

Using strWrite As New IO.StreamWriter (fileName, True)
strWrite.WriteL ine(Now & vbTab & cLineToWrite)
End Using

Should I explicitly call the Close method on the StreamWriter or is the
object closed and disposed when the Using block is exited?

Thanks for your insights.
As far as i know, "End Using" statement is enough to dispose resource.
No need to use close method in that case.
Apr 7 '08 #2
On 2008-04-07, JohnMSyrasoft <Jo***********@ discussions.mic rosoft.comwrote :
I'm coding in VB.NET 2005 with a Using block and a StreamWriter as in

Using strWrite As New IO.StreamWriter (fileName, True)
strWrite.WriteL ine(Now & vbTab & cLineToWrite)
End Using

Should I explicitly call the Close method on the StreamWriter or is the
object closed and disposed when the Using block is exited?

Thanks for your insights.
It is closed when the using block exits - even if there is an error,
that is the point of a using block. Calling close explicitly, just makes
for redundant code.

--
Tom Shelton
Apr 7 '08 #3

"Tom Shelton" <to*********@YO UKNOWTHEDRILLco mcast.netwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Calling close explicitly, just makes for redundant code.

--
Tom Shelton
Not necessarially. You may be done using the object, but not have hit the
"End Using" yet. With a resource like a file (that others may need to
access), it may be a good idea to call close() explicitly as soon as you are
done using it and use "End Using" as a fail-safe.

-Scott
Apr 7 '08 #4
On 2008-04-07, Scott M. <sm**@nospam.no spamwrote:
>
"Tom Shelton" <to*********@YO UKNOWTHEDRILLco mcast.netwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>Calling close explicitly, just makes for redundant code.

--
Tom Shelton

Not necessarially. You may be done using the object, but not have hit the
"End Using" yet. With a resource like a file (that others may need to
access), it may be a good idea to call close() explicitly as soon as you are
done using it and use "End Using" as a fail-safe.

-Scott

IMHO, that would be a rare case, and one that I would suggest is
probably in need of refactoring. In general, with files the rule is
get in, get what you need, and get out. No, in general, I would stand
by my original post.

--
Tom Shelton
Apr 7 '08 #5
IMHO, I don't think it's as rare as you may think. If I'm going to need to
re-open the stream after I close it, I wouldn't want it disposed yet, but I
would want it closed while I'm not using it.
"Tom Shelton" <to*********@YO UKNOWTHEDRILLco mcast.netwrote in message
news:eO******** ******@TK2MSFTN GP03.phx.gbl...
On 2008-04-07, Scott M. <sm**@nospam.no spamwrote:
>>
"Tom Shelton" <to*********@YO UKNOWTHEDRILLco mcast.netwrote in message
news:%2******* *********@TK2MS FTNGP03.phx.gbl ...
>>Calling close explicitly, just makes for redundant code.

--
Tom Shelton

Not necessarially. You may be done using the object, but not have hit
the
"End Using" yet. With a resource like a file (that others may need to
access), it may be a good idea to call close() explicitly as soon as you
are
done using it and use "End Using" as a fail-safe.

-Scott


IMHO, that would be a rare case, and one that I would suggest is
probably in need of refactoring. In general, with files the rule is
get in, get what you need, and get out. No, in general, I would stand
by my original post.

--
Tom Shelton

Apr 7 '08 #6

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

Similar topics

3
9095
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on the client machine to close per my network app the computer fills up w/ thousands of these :0 TCP foobox:8888 localhost:2188 TIME_WAIT :0 TCP foobox:8888 localhost:2189 TIME_WAIT :0 TCP foobox:8888 localhost:2190 TIME_WAIT
1
3800
by: Daniel | last post by:
System.IO.StreamWriter Close or Flush method to shut down the computer in such a way that just part of the file is written? or an empty file is written? Also if the Close or Flush is to a streamwriter writing to a network share, is it possible for the network to go down in such a way that the tartet file is only partialy written? or are there some kind of check sums to prevent this.
2
2942
by: Daniel | last post by:
if System.IO.StreamWriter write throws an exception, is there anyway to close the System.IO.StreamWriter object? it seems to stay open when this happens then future attempts to write to that same path fail because it says its in use by another process.
4
28353
by: Dan | last post by:
In the following example, is it necessary to close the FileStream object as well as the StreamWriter object? FileStream fs = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write, FileShare.None); StreamWriter swFromFile = new StreamWriter(logFile); swFromFile.Write(textToAdd); swFromFile.Flush(); swFromFile.Close();
1
1398
by: Michael R. Pierotti | last post by:
I have a app I have written that when the program is opened it logs out to a logfile. What I can't figure out is that "X" button at the top. If a user clicks on that instead of my menu item close how can I call the log sub and write a closed program log entry ? 'Add any initialization after the InitializeComponent() call CreateLFile() Dim myear As String = Date.Now.Year.ToString
4
3332
by: hharry | last post by:
Hi All, I have a simple web service: <WebMethod()> _ Public Function ReSample(ByVal sInput As Integer) As String
1
2716
by: hharry | last post by:
Hi All, I have asimple web service: <WebMethod()> _ Public Function ReSample(ByVal sInput As Integer) As String
7
9783
by: Eran.Yasso | last post by:
Hi, In the MSDN the sample doesn't use the close() method. But I know that in most languages you do need to use the close() method after reading and writing to a file. from MSDN: public static void Main() {
54
5202
by: Zytan | last post by:
I have a log class that makes a synchronized TextWriter like so, in the constructor: StreamWriter sw = new StreamWriter(filename); tw = TextWriter.Synchronized(sw); In the destructor, ~MyLogClass(), I call: tw.WriteLine("some stuff"); tw.Close();
0
8680
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7738
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.