473,388 Members | 979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Exception error occurs on second call to procedure

Hello,

The following code generates an exception error the *second* time it is
called.
Can anyone see what I'm doing wrong?
There is a similar example in Wrox Professional VB.NET Page 289.

Error:
An unhandled exception of type 'System.ObjectDisposedException'
occurred in mscorlib.dll
Additional information: Cannot write to a closed TextWriter.
The error occurs on this line: Debug.WriteLine("Date: " & DateTime.Now)
Code:
Dim LogFile As String = "C:\LogFile.txt"
Dim objWriter As IO.StreamWriter
objWriter = New IO.StreamWriter(LogFile, FileMode.OpenOrCreate)
Debug.Listeners.Add(New TextWriterTraceListener(objWriter))
Debug.WriteLine("Date: " & DateTime.Now)
Debug.Indent()
Debug.WriteLine("Error: " & Exception.Message)
objWriter.Flush()
objWriter.Close()
objWriter = Nothing

Thank you,
Paul
Nov 21 '05 #1
6 5762
Which line is the error occuring on?

"Paul" <pa**@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hello,

The following code generates an exception error the *second* time it is
called.
Can anyone see what I'm doing wrong?
There is a similar example in Wrox Professional VB.NET Page 289.

Error:
An unhandled exception of type 'System.ObjectDisposedException'
occurred in mscorlib.dll
Additional information: Cannot write to a closed TextWriter.
The error occurs on this line: Debug.WriteLine("Date: " & DateTime.Now)
Code:
Dim LogFile As String = "C:\LogFile.txt"
Dim objWriter As IO.StreamWriter
objWriter = New IO.StreamWriter(LogFile, FileMode.OpenOrCreate)
Debug.Listeners.Add(New TextWriterTraceListener(objWriter))
Debug.WriteLine("Date: " & DateTime.Now)
Debug.Indent()
Debug.WriteLine("Error: " & Exception.Message)
objWriter.Flush()
objWriter.Close()
objWriter = Nothing

Thank you,
Paul

Nov 21 '05 #2
Try:

Dim LogFile As String = "C:\LogFile.txt"
Dim objWriter As IO.StreamWriter
objWriter = New IO.StreamWriter(LogFile, FileMode.OpenOrCreate)
Dim myTextListener As New TextWriterTraceListener(objWriter) '
<<<<----- create a var you can ref
Debug.Listeners.Add(myTextListener)
Debug.WriteLine("Date: " & DateTime.Now)
Debug.Indent()
Debug.WriteLine("Error: test")
objWriter.Flush()
objWriter.Close()
Debug.Listeners.Remove(myTextListener) ' <<<<---- remove the ref

HTH,
Greg
"Paul" <pa**@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hello,

The following code generates an exception error the *second* time it is
called.
Can anyone see what I'm doing wrong?
There is a similar example in Wrox Professional VB.NET Page 289.

Error:
An unhandled exception of type 'System.ObjectDisposedException'
occurred in mscorlib.dll
Additional information: Cannot write to a closed TextWriter.
The error occurs on this line: Debug.WriteLine("Date: " & DateTime.Now)
Code:
Dim LogFile As String = "C:\LogFile.txt"
Dim objWriter As IO.StreamWriter
objWriter = New IO.StreamWriter(LogFile, FileMode.OpenOrCreate)
Debug.Listeners.Add(New TextWriterTraceListener(objWriter))
Debug.WriteLine("Date: " & DateTime.Now)
Debug.Indent()
Debug.WriteLine("Error: " & Exception.Message)
objWriter.Flush()
objWriter.Close()
objWriter = Nothing

Thank you,
Paul

Nov 21 '05 #3
That did it.

Thanks Greg
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:uR**************@TK2MSFTNGP10.phx.gbl...
Try:

Dim LogFile As String = "C:\LogFile.txt"
Dim objWriter As IO.StreamWriter
objWriter = New IO.StreamWriter(LogFile, FileMode.OpenOrCreate)
Dim myTextListener As New TextWriterTraceListener(objWriter) '
<<<<----- create a var you can ref
Debug.Listeners.Add(myTextListener)
Debug.WriteLine("Date: " & DateTime.Now)
Debug.Indent()
Debug.WriteLine("Error: test")
objWriter.Flush()
objWriter.Close()
Debug.Listeners.Remove(myTextListener) ' <<<<---- remove the ref

HTH,
Greg
"Paul" <pa**@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hello,

The following code generates an exception error the *second* time it is
called.
Can anyone see what I'm doing wrong?
There is a similar example in Wrox Professional VB.NET Page 289.

Error:
An unhandled exception of type 'System.ObjectDisposedException'
occurred in mscorlib.dll
Additional information: Cannot write to a closed TextWriter.
The error occurs on this line: Debug.WriteLine("Date: " & DateTime.Now)
Code:
Dim LogFile As String = "C:\LogFile.txt"
Dim objWriter As IO.StreamWriter
objWriter = New IO.StreamWriter(LogFile, FileMode.OpenOrCreate)
Debug.Listeners.Add(New TextWriterTraceListener(objWriter))
Debug.WriteLine("Date: " & DateTime.Now)
Debug.Indent()
Debug.WriteLine("Error: " & Exception.Message)
objWriter.Flush()
objWriter.Close()
objWriter = Nothing

Thank you,
Paul


Nov 21 '05 #4
Debug.Listeners.Add(New TextWriterTraceListener(objWriter))

So this would create a temporary unamed variable by the framework, which
upon rentering the loop would create it in the same variable name/address
space correct?

At least thats what appears to be happening.

Is this by design?
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:uR**************@TK2MSFTNGP10.phx.gbl...
Try:

Dim LogFile As String = "C:\LogFile.txt"
Dim objWriter As IO.StreamWriter
objWriter = New IO.StreamWriter(LogFile, FileMode.OpenOrCreate)
Dim myTextListener As New TextWriterTraceListener(objWriter) '
<<<<----- create a var you can ref
Debug.Listeners.Add(myTextListener)
Debug.WriteLine("Date: " & DateTime.Now)
Debug.Indent()
Debug.WriteLine("Error: test")
objWriter.Flush()
objWriter.Close()
Debug.Listeners.Remove(myTextListener) ' <<<<---- remove the ref

HTH,
Greg
"Paul" <pa**@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Hello,

The following code generates an exception error the *second* time it is
called.
Can anyone see what I'm doing wrong?
There is a similar example in Wrox Professional VB.NET Page 289.

Error:
An unhandled exception of type 'System.ObjectDisposedException'
occurred in mscorlib.dll
Additional information: Cannot write to a closed TextWriter.
The error occurs on this line: Debug.WriteLine("Date: " & DateTime.Now)
Code:
Dim LogFile As String = "C:\LogFile.txt"
Dim objWriter As IO.StreamWriter
objWriter = New IO.StreamWriter(LogFile, FileMode.OpenOrCreate)
Debug.Listeners.Add(New TextWriterTraceListener(objWriter))
Debug.WriteLine("Date: " & DateTime.Now)
Debug.Indent()
Debug.WriteLine("Error: " & Exception.Message)
objWriter.Flush()
objWriter.Close()
objWriter = Nothing

Thank you,
Paul


Nov 21 '05 #5
I dunno.

Even if you assigned it to a variable it would still has a problem when you
run the procedure a second time.

I just took a look at my own code and saw it was having the same problem as
Paul's when ran twice (our code is practically verbatim from the help file
on the subject).

I added a Trace.Listeners.Remove(myTextListener) and the problem went away.

Not sure why.

So much for looking like a code guru... :^)

Greg
"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Debug.Listeners.Add(New TextWriterTraceListener(objWriter))

So this would create a temporary unamed variable by the framework, which
upon rentering the loop would create it in the same variable name/address
space correct?

At least thats what appears to be happening.

Is this by design?
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:uR**************@TK2MSFTNGP10.phx.gbl...
Try:

Dim LogFile As String = "C:\LogFile.txt"
Dim objWriter As IO.StreamWriter
objWriter = New IO.StreamWriter(LogFile, FileMode.OpenOrCreate)
Dim myTextListener As New TextWriterTraceListener(objWriter) '
<<<<----- create a var you can ref
Debug.Listeners.Add(myTextListener)
Debug.WriteLine("Date: " & DateTime.Now)
Debug.Indent()
Debug.WriteLine("Error: test")
objWriter.Flush()
objWriter.Close()
Debug.Listeners.Remove(myTextListener) ' <<<<---- remove the ref

HTH,
Greg
"Paul" <pa**@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> Hello,
>
> The following code generates an exception error the *second* time it is
> called.
> Can anyone see what I'm doing wrong?
> There is a similar example in Wrox Professional VB.NET Page 289.
>
> Error:
> An unhandled exception of type 'System.ObjectDisposedException'
> occurred in mscorlib.dll
> Additional information: Cannot write to a closed TextWriter.
>
>
> The error occurs on this line: Debug.WriteLine("Date: " & DateTime.Now)
> Code:
> Dim LogFile As String = "C:\LogFile.txt"
> Dim objWriter As IO.StreamWriter
> objWriter = New IO.StreamWriter(LogFile, FileMode.OpenOrCreate)
> Debug.Listeners.Add(New TextWriterTraceListener(objWriter))
> Debug.WriteLine("Date: " & DateTime.Now)
> Debug.Indent()
> Debug.WriteLine("Error: " & Exception.Message)
> objWriter.Flush()
> objWriter.Close()
> objWriter = Nothing
>
> Thank you,
> Paul
>
>



Nov 21 '05 #6
Wasn't trying to do that... was just curious to know how it worked. =)

Thanks though!
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:%2*****************@TK2MSFTNGP11.phx.gbl...
I dunno.

Even if you assigned it to a variable it would still has a problem when you run the procedure a second time.

I just took a look at my own code and saw it was having the same problem as Paul's when ran twice (our code is practically verbatim from the help file
on the subject).

I added a Trace.Listeners.Remove(myTextListener) and the problem went away.
Not sure why.

So much for looking like a code guru... :^)

Greg
"CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Debug.Listeners.Add(New TextWriterTraceListener(objWriter))

So this would create a temporary unamed variable by the framework, which
upon rentering the loop would create it in the same variable name/address space correct?

At least thats what appears to be happening.

Is this by design?
"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:uR**************@TK2MSFTNGP10.phx.gbl...
Try:

Dim LogFile As String = "C:\LogFile.txt"
Dim objWriter As IO.StreamWriter
objWriter = New IO.StreamWriter(LogFile, FileMode.OpenOrCreate)
Dim myTextListener As New TextWriterTraceListener(objWriter) '
<<<<----- create a var you can ref
Debug.Listeners.Add(myTextListener)
Debug.WriteLine("Date: " & DateTime.Now)
Debug.Indent()
Debug.WriteLine("Error: test")
objWriter.Flush()
objWriter.Close()
Debug.Listeners.Remove(myTextListener) ' <<<<---- remove the ref
HTH,
Greg
"Paul" <pa**@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> Hello,
>
> The following code generates an exception error the *second* time it is > called.
> Can anyone see what I'm doing wrong?
> There is a similar example in Wrox Professional VB.NET Page 289.
>
> Error:
> An unhandled exception of type 'System.ObjectDisposedException'
> occurred in mscorlib.dll
> Additional information: Cannot write to a closed TextWriter.
>
>
> The error occurs on this line: Debug.WriteLine("Date: " & DateTime.Now) > Code:
> Dim LogFile As String = "C:\LogFile.txt"
> Dim objWriter As IO.StreamWriter
> objWriter = New IO.StreamWriter(LogFile, FileMode.OpenOrCreate)
> Debug.Listeners.Add(New TextWriterTraceListener(objWriter))
> Debug.WriteLine("Date: " & DateTime.Now)
> Debug.Indent()
> Debug.WriteLine("Error: " & Exception.Message)
> objWriter.Flush()
> objWriter.Close()
> objWriter = Nothing
>
> Thank you,
> Paul
>
>



Nov 21 '05 #7

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

Similar topics

2
by: Randy Harris | last post by:
I thought that I had a grasp of how VBA error handling functioned, but have just become painfully aware that I don't. I thought that the "On Error GoTo 0" in the second sub below would turn off...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
5
by: Peter Steele | last post by:
We have an application that when it runs in the IDE in debug mode an unhandled exception is occurring in a system header file associated with STL stirngs. The actual statement that crashes is ...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
4
by: Craig831 | last post by:
First off, I apologize if this gets long. I'm simply trying to give you all enough information to help me out. I'm writing (almost finished, actually), my first VB.Net application. It's a forms...
8
by: bole2cant | last post by:
When I add a Button to my NotifyIcon program I get the following: An unhandled exception of type 'System.NullReferenceException' occurred in unknown module. Additional information: Object...
7
by: ZorpiedoMan | last post by:
Well, I'm still wondering around in the "land of the lost exception"... It seems that controls that are bound to a class and the class throws an error in the SET method of the bound member, the...
1
by: | last post by:
I have an application that has a presentation later, business layer, and data layer. All three projects have their own exception policy, the "UI Policy", "BL Policy", "DL Policy", all of which...
9
by: Ratfish | last post by:
I'm getting a "2014:: Commands out of sync; you can't run this command now" error on a php page when I try to call a second stored procedure against a MySQL db. Does anyone know why I might be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.