Exception error occurs on second call to procedure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul

    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.ObjectD isposedExceptio n'
    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.OpenOr Create)
    Debug.Listeners .Add(New TextWriterTrace Listener(objWri ter))
    Debug.WriteLine ("Date: " & DateTime.Now)
    Debug.Indent()
    Debug.WriteLine ("Error: " & Exception.Messa ge)
    objWriter.Flush ()
    objWriter.Close ()
    objWriter = Nothing

    Thank you,
    Paul


  • CJ Taylor

    #2
    Re: Exception error occurs on second call to procedure

    Which line is the error occuring on?

    "Paul" <paul@nospam.co m> wrote in message
    news:%23fBI8BKp EHA.1960@TK2MSF TNGP10.phx.gbl. ..[color=blue]
    > 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.ObjectD isposedExceptio n'
    > 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.OpenOr Create)
    > Debug.Listeners .Add(New TextWriterTrace Listener(objWri ter))
    > Debug.WriteLine ("Date: " & DateTime.Now)
    > Debug.Indent()
    > Debug.WriteLine ("Error: " & Exception.Messa ge)
    > objWriter.Flush ()
    > objWriter.Close ()
    > objWriter = Nothing
    >
    > Thank you,
    > Paul
    >
    >[/color]


    Comment

    • Greg Burns

      #3
      Re: Exception error occurs on second call to procedure

      Try:

      Dim LogFile As String = "C:\LogFile.txt "
      Dim objWriter As IO.StreamWriter
      objWriter = New IO.StreamWriter (LogFile, FileMode.OpenOr Create)
      Dim myTextListener As New TextWriterTrace Listener(objWri ter) '
      <<<<----- create a var you can ref
      Debug.Listeners .Add(myTextList ener)
      Debug.WriteLine ("Date: " & DateTime.Now)
      Debug.Indent()
      Debug.WriteLine ("Error: test")
      objWriter.Flush ()
      objWriter.Close ()
      Debug.Listeners .Remove(myTextL istener) ' <<<<---- remove the ref

      HTH,
      Greg


      "Paul" <paul@nospam.co m> wrote in message
      news:%23fBI8BKp EHA.1960@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > 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.ObjectD isposedExceptio n'
      > 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.OpenOr Create)
      > Debug.Listeners .Add(New TextWriterTrace Listener(objWri ter))
      > Debug.WriteLine ("Date: " & DateTime.Now)
      > Debug.Indent()
      > Debug.WriteLine ("Error: " & Exception.Messa ge)
      > objWriter.Flush ()
      > objWriter.Close ()
      > objWriter = Nothing
      >
      > Thank you,
      > Paul
      >
      >[/color]


      Comment

      • Paul

        #4
        Re: Exception error occurs on second call to procedure

        That did it.

        Thanks Greg


        "Greg Burns" <greg_burns@DON T_SPAM_ME_hotma il.com> wrote in message
        news:uRYx91LpEH A.1960@TK2MSFTN GP10.phx.gbl...[color=blue]
        > Try:
        >
        > Dim LogFile As String = "C:\LogFile.txt "
        > Dim objWriter As IO.StreamWriter
        > objWriter = New IO.StreamWriter (LogFile, FileMode.OpenOr Create)
        > Dim myTextListener As New TextWriterTrace Listener(objWri ter) '
        > <<<<----- create a var you can ref
        > Debug.Listeners .Add(myTextList ener)
        > Debug.WriteLine ("Date: " & DateTime.Now)
        > Debug.Indent()
        > Debug.WriteLine ("Error: test")
        > objWriter.Flush ()
        > objWriter.Close ()
        > Debug.Listeners .Remove(myTextL istener) ' <<<<---- remove the ref
        >
        > HTH,
        > Greg
        >
        >
        > "Paul" <paul@nospam.co m> wrote in message
        > news:%23fBI8BKp EHA.1960@TK2MSF TNGP10.phx.gbl. ..[color=green]
        >> 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.ObjectD isposedExceptio n'
        >> 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.OpenOr Create)
        >> Debug.Listeners .Add(New TextWriterTrace Listener(objWri ter))
        >> Debug.WriteLine ("Date: " & DateTime.Now)
        >> Debug.Indent()
        >> Debug.WriteLine ("Error: " & Exception.Messa ge)
        >> objWriter.Flush ()
        >> objWriter.Close ()
        >> objWriter = Nothing
        >>
        >> Thank you,
        >> Paul
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • CJ Taylor

          #5
          Re: Exception error occurs on second call to procedure

          Debug.Listeners .Add(New TextWriterTrace Listener(objWri ter))

          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@DON T_SPAM_ME_hotma il.com> wrote in message
          news:uRYx91LpEH A.1960@TK2MSFTN GP10.phx.gbl...[color=blue]
          > Try:
          >
          > Dim LogFile As String = "C:\LogFile.txt "
          > Dim objWriter As IO.StreamWriter
          > objWriter = New IO.StreamWriter (LogFile, FileMode.OpenOr Create)
          > Dim myTextListener As New TextWriterTrace Listener(objWri ter) '
          > <<<<----- create a var you can ref
          > Debug.Listeners .Add(myTextList ener)
          > Debug.WriteLine ("Date: " & DateTime.Now)
          > Debug.Indent()
          > Debug.WriteLine ("Error: test")
          > objWriter.Flush ()
          > objWriter.Close ()
          > Debug.Listeners .Remove(myTextL istener) ' <<<<---- remove the ref
          >
          > HTH,
          > Greg
          >
          >
          > "Paul" <paul@nospam.co m> wrote in message
          > news:%23fBI8BKp EHA.1960@TK2MSF TNGP10.phx.gbl. ..[color=green]
          > > 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.ObjectD isposedExceptio n'
          > > 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.OpenOr Create)
          > > Debug.Listeners .Add(New TextWriterTrace Listener(objWri ter))
          > > Debug.WriteLine ("Date: " & DateTime.Now)
          > > Debug.Indent()
          > > Debug.WriteLine ("Error: " & Exception.Messa ge)
          > > objWriter.Flush ()
          > > objWriter.Close ()
          > > objWriter = Nothing
          > >
          > > Thank you,
          > > Paul
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Greg Burns

            #6
            Re: Exception error occurs on second call to procedure

            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(myTextL istener) 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:%23ZqOW4Mp EHA.2180@TK2MSF TNGP10.phx.gbl. ..[color=blue]
            > Debug.Listeners .Add(New TextWriterTrace Listener(objWri ter))
            >
            > 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@DON T_SPAM_ME_hotma il.com> wrote in message
            > news:uRYx91LpEH A.1960@TK2MSFTN GP10.phx.gbl...[color=green]
            >> Try:
            >>
            >> Dim LogFile As String = "C:\LogFile.txt "
            >> Dim objWriter As IO.StreamWriter
            >> objWriter = New IO.StreamWriter (LogFile, FileMode.OpenOr Create)
            >> Dim myTextListener As New TextWriterTrace Listener(objWri ter) '
            >> <<<<----- create a var you can ref
            >> Debug.Listeners .Add(myTextList ener)
            >> Debug.WriteLine ("Date: " & DateTime.Now)
            >> Debug.Indent()
            >> Debug.WriteLine ("Error: test")
            >> objWriter.Flush ()
            >> objWriter.Close ()
            >> Debug.Listeners .Remove(myTextL istener) ' <<<<---- remove the ref
            >>
            >> HTH,
            >> Greg
            >>
            >>
            >> "Paul" <paul@nospam.co m> wrote in message
            >> news:%23fBI8BKp EHA.1960@TK2MSF TNGP10.phx.gbl. ..[color=darkred]
            >> > 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.ObjectD isposedExceptio n'
            >> > 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.OpenOr Create)
            >> > Debug.Listeners .Add(New TextWriterTrace Listener(objWri ter))
            >> > Debug.WriteLine ("Date: " & DateTime.Now)
            >> > Debug.Indent()
            >> > Debug.WriteLine ("Error: " & Exception.Messa ge)
            >> > objWriter.Flush ()
            >> > objWriter.Close ()
            >> > objWriter = Nothing
            >> >
            >> > Thank you,
            >> > Paul
            >> >
            >> >[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • CJ Taylor

              #7
              Re: Exception error occurs on second call to procedure

              Wasn't trying to do that... was just curious to know how it worked. =)

              Thanks though!


              "Greg Burns" <greg_burns@DON T_SPAM_ME_hotma il.com> wrote in message
              news:%23BIyn%23 MpEHA.592@TK2MS FTNGP11.phx.gbl ...[color=blue]
              > I dunno.
              >
              > Even if you assigned it to a variable it would still has a problem when[/color]
              you[color=blue]
              > run the procedure a second time.
              >
              > I just took a look at my own code and saw it was having the same problem[/color]
              as[color=blue]
              > Paul's when ran twice (our code is practically verbatim from the help file
              > on the subject).
              >
              > I added a Trace.Listeners .Remove(myTextL istener) and the problem went[/color]
              away.[color=blue]
              >
              > Not sure why.
              >
              > So much for looking like a code guru... :^)
              >
              > Greg
              >
              >
              > "CJ Taylor" <[cege] at [tavayn] dit commmmm> wrote in message
              > news:%23ZqOW4Mp EHA.2180@TK2MSF TNGP10.phx.gbl. ..[color=green]
              > > Debug.Listeners .Add(New TextWriterTrace Listener(objWri ter))
              > >
              > > So this would create a temporary unamed variable by the framework, which
              > > upon rentering the loop would create it in the same variable[/color][/color]
              name/address[color=blue][color=green]
              > > space correct?
              > >
              > > At least thats what appears to be happening.
              > >
              > > Is this by design?
              > >
              > >
              > > "Greg Burns" <greg_burns@DON T_SPAM_ME_hotma il.com> wrote in message
              > > news:uRYx91LpEH A.1960@TK2MSFTN GP10.phx.gbl...[color=darkred]
              > >> Try:
              > >>
              > >> Dim LogFile As String = "C:\LogFile.txt "
              > >> Dim objWriter As IO.StreamWriter
              > >> objWriter = New IO.StreamWriter (LogFile, FileMode.OpenOr Create)
              > >> Dim myTextListener As New TextWriterTrace Listener(objWri ter) '
              > >> <<<<----- create a var you can ref
              > >> Debug.Listeners .Add(myTextList ener)
              > >> Debug.WriteLine ("Date: " & DateTime.Now)
              > >> Debug.Indent()
              > >> Debug.WriteLine ("Error: test")
              > >> objWriter.Flush ()
              > >> objWriter.Close ()
              > >> Debug.Listeners .Remove(myTextL istener) ' <<<<---- remove the[/color][/color][/color]
              ref[color=blue][color=green][color=darkred]
              > >>
              > >> HTH,
              > >> Greg
              > >>
              > >>
              > >> "Paul" <paul@nospam.co m> wrote in message
              > >> news:%23fBI8BKp EHA.1960@TK2MSF TNGP10.phx.gbl. ..
              > >> > Hello,
              > >> >
              > >> > The following code generates an exception error the *second* time it[/color][/color][/color]
              is[color=blue][color=green][color=darkred]
              > >> > 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.ObjectD isposedExceptio n'
              > >> > occurred in mscorlib.dll
              > >> > Additional information: Cannot write to a closed TextWriter.
              > >> >
              > >> >
              > >> > The error occurs on this line: Debug.WriteLine ("Date: " &[/color][/color][/color]
              DateTime.Now)[color=blue][color=green][color=darkred]
              > >> > Code:
              > >> > Dim LogFile As String = "C:\LogFile.txt "
              > >> > Dim objWriter As IO.StreamWriter
              > >> > objWriter = New IO.StreamWriter (LogFile, FileMode.OpenOr Create)
              > >> > Debug.Listeners .Add(New TextWriterTrace Listener(objWri ter))
              > >> > Debug.WriteLine ("Date: " & DateTime.Now)
              > >> > Debug.Indent()
              > >> > Debug.WriteLine ("Error: " & Exception.Messa ge)
              > >> > objWriter.Flush ()
              > >> > objWriter.Close ()
              > >> > objWriter = Nothing
              > >> >
              > >> > Thank you,
              > >> > Paul
              > >> >
              > >> >
              > >>
              > >>[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              Working...