By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,886 Members | 1,249 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,886 IT Pros & Developers. It's quick & easy.

Exception error occurs on second call to procedure

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.