473,748 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How Do I: 'System.ObjectD isposedExceptio n'

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
Jun 27 '08 #1
5 4977
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
Jun 27 '08 #2
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.
Jun 27 '08 #3
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
Jun 27 '08 #4
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" <Li*********@gm ail.comwrote in message
news:81******** *************** ***********@m71 g2000hse.google groups.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
Jun 27 '08 #5
Brian-,

Why not simple ?

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

-Cor
Jun 27 '08 #6

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

Similar topics

0
1297
by: TomPel | last post by:
I have various processes that are all logging to the same file using the same function. In order to ensure nobody logs at the same time, I am using mutex. The code I am using is listed below: mut.WaitOne() Dim l_fstream As New StreamWriter(l_logfile, True) l_fstream.WriteLine(stringtolog) l_fstream.Flush() l_fstream.Close() mut.ReleaseMutex()
3
15317
by: MJB | last post by:
I'm getting an IStream back from function xmlHttp.responsestream. I would like to convert this to a System.IO.Stream in order to work with it in my application. Has anyone encountered this and written a conversion? TIA, Matt
0
3785
by: Richard Blewett [DevelopMentor] | last post by:
In theory there is nothing wrong with the Windows Forms part of your application (using your code as a start I mocked up the app and can happily press the connect and disconnect buttons ad-infinitum). So can you put a try catch handler in the Disconnect_Click handler to trap the exception so you can, say, write the exception yttace out to a file. You need to know which obejct is generating the exception, whether it is one of your Windows Forms...
0
2598
by: Naveen | last post by:
Hello all, Environment - Winforms app calling into a webservice Exception returned - "A first chance exception of type 'System.ObjectDisposedException' occurred in system.dll" Additional information: Cannot access a disposed object named "System.Net.Sockets.NetworkStream". We are making a webmethod call that gets back a simple ID
9
17767
by: SharpCoderMP | last post by:
i've been experiencing random crashes on some machines running my app. the app never crashed in such way on my dev machine so i'm totally unable to debug this. the error page users get says nothing special: EventType : clr20r3 P1 : app.exe P2 : 1.0.0.0 P3 : 24 P4 : app P5 : 1.0.0.0 P6 : 24 P7 : 11d P8 : 0 P9 : system.nullreferenceexception
3
8750
by: Martin Pöpping | last post by:
Hello, I´ve a problem with a small line of code. For my Windows Application I designed a form for exporting data into xml files. I´m opening the dialog with: ExportDialog export = new ExportDialog(); export.Show();
1
3937
by: Treadstone | last post by:
Hi All, I am writing an application in C# (.NET CF) to generate a vCard and transmit it over bluetooth. To do this, on the menu item of a form, i generate an "On click Menu Item" event and that will take me to a new form. In this new form I use the ChooseContactDialog class to obtain a contact name from the phone book and store it into a text box. However, I am getting a System.ObjectDisposed Exception when it enters into the "On Click...
2
3941
by: Bernard Borsu | last post by:
Hi ! My website breakdown at least 20 times per day with a System.ObjectDisposedException. The only information available are those given by the httpmodule treating the unhandledexception causing breakdown. Here are these information : Type de l'événement : Erreur Source de l'événement : ASP.NET 2.0.50727.0
0
3062
by: sdanda | last post by:
Hai i am working on vb.net. In my application I created four forms.Those are first.vb,f1.vb,f2.vb and f3.vb In firstvb I added 3 checkboxes and a "display" button.The 3 checkboxes are used to display the forms f1,f2 and f3.If first checkbox is checked then it displays the f1 form.Similarly for f2 and f3 also.I kept all this code in the display button click event.In f1.vb,f2.vb and f3.vb I added back and next buttons. I wrote some code to...
0
8995
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9378
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9253
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6077
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2216
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.