Re: How Do I: 'System.ObjectDisposedException'

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



On Apr 14, 8:25 pm, kimiraikkonen <kimiraikkone...@xxxxxxxxx> wrote:
On Apr 14, 8:09 pm, Brian <LineSupp...@xxxxxxxxx> wrote:



Hello all.

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

An exception of type 'System.ObjectDisposedException' 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.Timer
Dim StartTime As Date
Dim BlockWindowStart As Date
Dim BlockWindowEnd As Date

Private Sub formMain_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
UpdateTmr = New System.Timers.Timer
UpdateTmr.Interval = 1000
UpdateTmr.Enabled = True
StartTime = DateTime.Now
BlockWindowStart = StartTime.AddSeconds(10)
BlockWindowEnd = BlockWindowStart.AddSeconds(10)

End Sub

Private Sub UpdateTmr_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles UpdateTmr.Elapsed
'Exception is thrown
here.<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Me.Invoke(New delUpdateForm(AddressOf UpdateForm))
End Sub

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

Dim ts As TimeSpan = DateTime.Now - BlockWindowStart
Debug.Print(ts.Ticks)
Debug.Print(DateTime.Now >= BlockWindowStart 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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Timer1.Enabled = True

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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.
.



Relevant Pages

  • How Do I: System.ObjectDisposedException
    ... Dim WithEvents UpdateTmr As System.Timers.Timer ... Dim BlockWindowStart As Date ... Private Sub formMain_Load(ByVal sender As System.Object, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How Do I: System.ObjectDisposedException
    ... Dim WithEvents UpdateTmr As System.Timers.Timer ... Dim BlockWindowStart As Date ... Private Sub formMain_Load(ByVal sender As System.Object, ... Private Sub UpdateTmr_Elapsed(ByVal sender As Object, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How Do I: System.ObjectDisposedException
    ... Private Sub UpdateTmr_ElapsedHandles UpdateTmr.Elapsed ... Me.Invoke(New delUpdateForm(AddressOf UpdateForm)) ... Dim WithEvents UpdateTmr As System.Timers.Timer ... Dim BlockWindowStart As Date ...
    (microsoft.public.dotnet.languages.vb)
  • Project Error
    ... Private Declare Sub Sleep Lib "Kernel32" ... Dim strDataSrc As String ...
    (microsoft.public.vb.bugs)
  • Re: How Much Longer Can SRians Ignore Their Fundamental Error.
    ... ecc As Variant ... Dim lightspeed, position, circlerun, Pref, prefmod As Integer, ... Private Sub Combo12_Change ... Erase Vangle ...
    (sci.astro)