How Do I: 'System.ObjectDisposedException'

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

    How Do I: 'System.ObjectDisposedException'

    Hello all.

    I have a simple application that I was using to test and understand
    the System.Timers.T imer and noticed that when I stop the application,
    on occasion, it throughs the following exception.

    An exception of type 'System.ObjectD isposedExceptio n' occurred in
    System.Windows. Forms.dll but was not handled in user code

    Additional information: Cannot access a disposed object.

    Should I just place the offending line in a Try Catch or set the timer
    to nothing in FormClosing. Below is the code, real short.


    Public Class formMain
    Dim c As New Chronology
    Dim WithEvents UpdateTmr As System.Timers.T imer
    Dim StartTime As Date
    Dim BlockWindowStar t As Date
    Dim BlockWindowEnd As Date



    Private Sub formMain_Load(B yVal sender As System.Object, ByVal e
    As System.EventArg s) Handles MyBase.Load
    UpdateTmr = New System.Timers.T imer
    UpdateTmr.Inter val = 1000
    UpdateTmr.Enabl ed = True
    StartTime = DateTime.Now
    BlockWindowStar t = StartTime.AddSe conds(10)
    BlockWindowEnd = BlockWindowStar t.AddSeconds(10 )

    End Sub


    Private Sub UpdateTmr_Elaps ed(ByVal sender As Object, ByVal e As
    System.Timers.E lapsedEventArgs ) Handles UpdateTmr.Elaps ed
    'Exception is thrown
    here.<<<<<<<<<< <<<<<<<<<<<<<<< <<<<<<<<<<<<<<< <<<<<<
    Me.Invoke(New delUpdateForm(A ddressOf UpdateForm))
    End Sub

    Public Delegate Sub delUpdateForm()
    Public Sub UpdateForm()
    Label1.Text = Now
    Dim x As DateTime = DateTime.Now
    Dim tsw As TimeSpan = BlockWindowEnd - BlockWindowStar t
    Debug.Print("Bl ock Window Value: " & tsw.Ticks)

    Dim ts As TimeSpan = DateTime.Now - BlockWindowStar t
    Debug.Print(ts. Ticks)
    Debug.Print(Dat eTime.Now >= BlockWindowStar t And DateTime.Now
    <= BlockWindowEnd)


    End Sub
    End Class
  • kimiraikkonen

    #2
    Re: How Do I: 'System.ObjectD isposedExceptio n'

    On Apr 14, 8:09 pm, Brian <LineSupp...@gm ail.comwrote:
    Hello all.
    >
    I have a simple application that I was using to test and understand
    the System.Timers.T imer and noticed that when I stop the application,
    on occasion, it throughs the following exception.
    >
    An exception of type 'System.ObjectD isposedExceptio n' occurred in
    System.Windows. Forms.dll but was not handled in user code
    >
    Additional information: Cannot access a disposed object.
    >
    Should I just place the offending line in a Try Catch or set the timer
    to nothing in FormClosing. Below is the code, real short.
    >
    Public Class formMain
    Dim c As New Chronology
    Dim WithEvents UpdateTmr As System.Timers.T imer
    Dim StartTime As Date
    Dim BlockWindowStar t As Date
    Dim BlockWindowEnd As Date
    >
    Private Sub formMain_Load(B yVal sender As System.Object, ByVal e
    As System.EventArg s) Handles MyBase.Load
    UpdateTmr = New System.Timers.T imer
    UpdateTmr.Inter val = 1000
    UpdateTmr.Enabl ed = True
    StartTime = DateTime.Now
    BlockWindowStar t = StartTime.AddSe conds(10)
    BlockWindowEnd = BlockWindowStar t.AddSeconds(10 )
    >
    End Sub
    >
    Private Sub UpdateTmr_Elaps ed(ByVal sender As Object, ByVal e As
    System.Timers.E lapsedEventArgs ) Handles UpdateTmr.Elaps ed
    'Exception is thrown
    here.<<<<<<<<<< <<<<<<<<<<<<<<< <<<<<<<<<<<<<<< <<<<<<
    Me.Invoke(New delUpdateForm(A ddressOf UpdateForm))
    End Sub
    >
    Public Delegate Sub delUpdateForm()
    Public Sub UpdateForm()
    Label1.Text = Now
    Dim x As DateTime = DateTime.Now
    Dim tsw As TimeSpan = BlockWindowEnd - BlockWindowStar t
    Debug.Print("Bl ock Window Value: " & tsw.Ticks)
    >
    Dim ts As TimeSpan = DateTime.Now - BlockWindowStar t
    Debug.Print(ts. Ticks)
    Debug.Print(Dat eTime.Now >= BlockWindowStar t And DateTime.Now
    <= BlockWindowEnd)
    >
    End Sub
    End Class
    Brian,
    I tried your code and works fine if you remove the unused class called
    "Chronology " at the top of your codepage, because i get "Chronology
    not defined" error which you might referenced to an exernal library.

    So, i noticed that your code presents the current date and time in the
    progress of being updated.

    Would you mind using this instead of the code you have if you're still
    having difficulty?

    Place a timer control named timer1 then paste the following code:

    ' -------------- Begin ------------

    Public Class Form1

    Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load

    Timer1.Enabled = True

    End Sub

    Private Sub Timer1_Tick(ByV al sender As System.Object, ByVal e As
    System.EventArg s) Handles Timer1.Tick

    Label1.Text = DateString + " " + TimeString

    End Sub

    End Class

    ' ------------- End ----------

    As i noticed that does pretty same job with your code.

    Hope this helps,

    Onur Güzel

    Comment

    • kimiraikkonen

      #3
      Re: How Do I: 'System.ObjectD isposedExceptio n'

      On Apr 14, 8:25 pm, kimiraikkonen <kimiraikkone.. .@gmail.comwrot e:
      On Apr 14, 8:09 pm, Brian <LineSupp...@gm ail.comwrote:
      >
      >
      >
      Hello all.
      >
      I have a simple application that I was using to test and understand
      the System.Timers.T imer and noticed that when I stop the application,
      on occasion, it throughs the following exception.
      >
      An exception of type 'System.ObjectD isposedExceptio n' occurred in
      System.Windows. Forms.dll but was not handled in user code
      >
      Additional information: Cannot access a disposed object.
      >
      Should I just place the offending line in a Try Catch or set the timer
      to nothing in FormClosing. Below is the code, real short.
      >
      Public Class formMain
      Dim c As New Chronology
      Dim WithEvents UpdateTmr As System.Timers.T imer
      Dim StartTime As Date
      Dim BlockWindowStar t As Date
      Dim BlockWindowEnd As Date
      >
      Private Sub formMain_Load(B yVal sender As System.Object, ByVal e
      As System.EventArg s) Handles MyBase.Load
      UpdateTmr = New System.Timers.T imer
      UpdateTmr.Inter val = 1000
      UpdateTmr.Enabl ed = True
      StartTime = DateTime.Now
      BlockWindowStar t = StartTime.AddSe conds(10)
      BlockWindowEnd = BlockWindowStar t.AddSeconds(10 )
      >
      End Sub
      >
      Private Sub UpdateTmr_Elaps ed(ByVal sender As Object, ByVal e As
      System.Timers.E lapsedEventArgs ) Handles UpdateTmr.Elaps ed
      'Exception is thrown
      here.<<<<<<<<<< <<<<<<<<<<<<<<< <<<<<<<<<<<<<<< <<<<<<
      Me.Invoke(New delUpdateForm(A ddressOf UpdateForm))
      End Sub
      >
      Public Delegate Sub delUpdateForm()
      Public Sub UpdateForm()
      Label1.Text = Now
      Dim x As DateTime = DateTime.Now
      Dim tsw As TimeSpan = BlockWindowEnd - BlockWindowStar t
      Debug.Print("Bl ock Window Value: " & tsw.Ticks)
      >
      Dim ts As TimeSpan = DateTime.Now - BlockWindowStar t
      Debug.Print(ts. Ticks)
      Debug.Print(Dat eTime.Now >= BlockWindowStar t And DateTime.Now
      <= BlockWindowEnd)
      >
      End Sub
      End Class
      >
      Brian,
      I tried your code and works fine if you remove the unused class called
      "Chronology " at the top of your codepage, because i get "Chronology
      not defined" error which you might referenced to an exernal library.
      >
      So, i noticed that your code presents the current date and time in the
      progress of being updated.
      >
      Would you mind using this instead of the code you have if you're still
      having difficulty?
      >
      Place a timer control named timer1 then paste the following code:
      >
      ' -------------- Begin ------------
      >
      Public Class Form1
      >
      Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
      System.EventArg s) Handles MyBase.Load
      >
      Timer1.Enabled = True
      >
      End Sub
      >
      Private Sub Timer1_Tick(ByV al sender As System.Object, ByVal e As
      System.EventArg s) Handles Timer1.Tick
      >
      Label1.Text = DateString + " " + TimeString
      >
      End Sub
      >
      End Class
      >
      ' ------------- End ----------
      >
      As i noticed that does pretty same job with your code.
      >
      Hope this helps,
      >
      Onur Güzel
      Note, don't forget to set timer's interval to 1000 miliseconds.

      Comment

      • Brian

        #4
        Re: How Do I: 'System.ObjectD isposedExceptio n'

        On Apr 14, 1:28 pm, kimiraikkonen <kimiraikkone.. .@gmail.comwrot e:
        On Apr 14, 8:25 pm, kimiraikkonen <kimiraikkone.. .@gmail.comwrot e:
        >
        >
        >
        >
        >
        On Apr 14, 8:09 pm, Brian <LineSupp...@gm ail.comwrote:
        >
        Hello all.
        >
        I have a simple application that I was using to test and understand
        the System.Timers.T imer and noticed that when I stop the application,
        on occasion, it throughs the following exception.
        >
        An exception of type 'System.ObjectD isposedExceptio n' occurred in
        System.Windows. Forms.dll but was not handled in user code
        >
        Additional information: Cannot access a disposed object.
        >
        Should I just place the offending line in a Try Catch or set the timer
        to nothing in FormClosing. Below is the code, real short.
        >
        Public Class formMain
            Dim c As New Chronology
            Dim WithEvents UpdateTmr As System.Timers.T imer
            Dim StartTime As Date
            Dim BlockWindowStar t As Date
            Dim BlockWindowEnd As Date
        >
            Private Sub formMain_Load(B yVal sender As System.Object, ByVale
        As System.EventArg s) Handles MyBase.Load
                UpdateTmr = New System.Timers.T imer
                UpdateTmr.Inter val = 1000
                UpdateTmr.Enabl ed = True
                StartTime = DateTime.Now
                BlockWindowStar t = StartTime.AddSe conds(10)
                BlockWindowEnd = BlockWindowStar t.AddSeconds(10 )
        >
            End Sub
        >
            Private Sub UpdateTmr_Elaps ed(ByVal sender As Object, ByVal e As
        System.Timers.E lapsedEventArgs ) Handles UpdateTmr.Elaps ed
        'Exception is thrown
        here.<<<<<<<<<< <<<<<<<<<<<<<<< <<<<<<<<<<<<<<< <<<<<<
                Me.Invoke(New delUpdateForm(A ddressOf UpdateForm))
            End Sub
        >
            Public Delegate Sub delUpdateForm()
            Public Sub UpdateForm()
                Label1.Text = Now
                Dim x As DateTime = DateTime.Now
                Dim tsw As TimeSpan = BlockWindowEnd - BlockWindowStar t
                Debug.Print("Bl ock Window Value: " & tsw.Ticks)
        >
                Dim ts As TimeSpan = DateTime.Now - BlockWindowStar t
                Debug.Print(ts. Ticks)
                Debug.Print(Dat eTime.Now >= BlockWindowStar t And DateTime.Now
        <= BlockWindowEnd)
        >
            End Sub
        End Class
        >
        Brian,
        I tried your code and works fine if you remove the unused class called
        "Chronology " at the top of your codepage, because i get "Chronology
        not defined" error which you might referenced to an exernal library.
        >
        So, i noticed that your code presents the current date and time in the
        progress of being updated.
        >
        Would you mind using this instead of the code you have if you're still
        having difficulty?
        >
        Place a timer control named timer1 then paste the following code:
        >
        ' -------------- Begin ------------
        >
        Public Class Form1
        >
        Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
        System.EventArg s) Handles MyBase.Load
        >
        Timer1.Enabled = True
        >
        End Sub
        >
        Private Sub Timer1_Tick(ByV al sender As System.Object, ByVal e As
        System.EventArg s) Handles Timer1.Tick
        >
        Label1.Text = DateString + " " + TimeString
        >
        End Sub
        >
        End Class
        >
        ' ------------- End ----------
        >
        As i noticed that does pretty same job with your code.
        >
        Hope this helps,
        >
        Onur Güzel
        >
        Note, don't forget to set timer's interval to 1000 miliseconds.- Hide quoted text -
        >
        - Show quoted text -
        Sorry, the Chronology class is empty at the moment, I intended to put
        all of the code dealing with dates or times in this class. Just didn't
        get that far before the exception happened.

        Thanks for the suggestions though. I would prefer to stay away from
        the Windows Forms Timer because eventually I want this to be a class.
        But just to clarify, the exception is not thrown each time I close the
        app, just occasionaly. I believe that because the Timer is running in
        it's own thread, the call back can occur as the form is closing, after
        the form has been disposed, so I get the error.

        Thanks


        Comment

        • Stephany Young

          #5
          Re: How Do I: 'System.ObjectD isposedExceptio n'

          Two ways to deal with it immediately to mind:

          1.
          Private Sub UpdateTmr_Elaps ed(ByVal sender As Object, ByVal e As
          System.Timers.E lapsedEventArgs ) Handles UpdateTmr.Elaps ed

          Try
          Me.Invoke(New delUpdateForm(A ddressOf UpdateForm))
          Catch _ex As ObjectDisposedE xception
          ' Take no action
          Catch _ex As Exception
          ' Deal with this situation
          End Try
          End Sub

          2.
          Handle the FormClosing event for the form and, in it:

          UpdateTmr.Enabl ed = False

          Using the first example means that you are dealing with the exception.

          Using the second example means that you are stopping the exception from
          occuring.


          "Brian" <LineSupport@gm ail.comwrote in message
          news:81a70763-01b5-491b-a14a-2dd8ca57e512@m7 1g2000hse.googl egroups.com...
          Hello all.
          >
          I have a simple application that I was using to test and understand
          the System.Timers.T imer and noticed that when I stop the application,
          on occasion, it throughs the following exception.
          >
          An exception of type 'System.ObjectD isposedExceptio n' occurred in
          System.Windows. Forms.dll but was not handled in user code
          >
          Additional information: Cannot access a disposed object.
          >
          Should I just place the offending line in a Try Catch or set the timer
          to nothing in FormClosing. Below is the code, real short.
          >
          >
          Public Class formMain
          Dim c As New Chronology
          Dim WithEvents UpdateTmr As System.Timers.T imer
          Dim StartTime As Date
          Dim BlockWindowStar t As Date
          Dim BlockWindowEnd As Date
          >
          >
          >
          Private Sub formMain_Load(B yVal sender As System.Object, ByVal e
          As System.EventArg s) Handles MyBase.Load
          UpdateTmr = New System.Timers.T imer
          UpdateTmr.Inter val = 1000
          UpdateTmr.Enabl ed = True
          StartTime = DateTime.Now
          BlockWindowStar t = StartTime.AddSe conds(10)
          BlockWindowEnd = BlockWindowStar t.AddSeconds(10 )
          >
          End Sub
          >
          >
          Private Sub UpdateTmr_Elaps ed(ByVal sender As Object, ByVal e As
          System.Timers.E lapsedEventArgs ) Handles UpdateTmr.Elaps ed
          'Exception is thrown
          here.<<<<<<<<<< <<<<<<<<<<<<<<< <<<<<<<<<<<<<<< <<<<<<
          Me.Invoke(New delUpdateForm(A ddressOf UpdateForm))
          End Sub
          >
          Public Delegate Sub delUpdateForm()
          Public Sub UpdateForm()
          Label1.Text = Now
          Dim x As DateTime = DateTime.Now
          Dim tsw As TimeSpan = BlockWindowEnd - BlockWindowStar t
          Debug.Print("Bl ock Window Value: " & tsw.Ticks)
          >
          Dim ts As TimeSpan = DateTime.Now - BlockWindowStar t
          Debug.Print(ts. Ticks)
          Debug.Print(Dat eTime.Now >= BlockWindowStar t And DateTime.Now
          <= BlockWindowEnd)
          >
          >
          End Sub
          End Class

          Comment

          • Cor Ligthert[MVP]

            #6
            Re: How Do I: 'System.ObjectD isposedExceptio n'

            Brian-,

            Why not simple ?

            \\\
            Private WithEvents UpdateTmr As New System.Timers.T imer
            ///

            -Cor

            Comment

            Working...