-
Oct 18th, 2012, 01:56 PM
#1
Thread Starter
Addicted Member
Do timers continue to tick after an object instance is disposed?
I have a subapp within my app. This subapp happens to be a chatting app, which constantly makes calls to a server. In addition, I have another subapp that is a posting station that also makes calls. Yet, whenever either of these apps are accessed, the program becomes unstable and slow, even after the app (which is a custom control) has been removed from their parent container. My question is... do the timers and thread workers that make these calls continue to run after the instance has been removed? What could be making my program so slow? Please note that these custom control "apps" are declared in the main form via global variables that can be accessed across all forms.
Always looking to meet fellow programmers. Message me if you want. Happy to help in whatever ways I can
-
Oct 18th, 2012, 02:01 PM
#2
Re: Do timers continue to tick after an object instance is disposed?
I was going to say that removing a control from the parent doesn't dispose of anything, but you aren't actually talking about controls. When it comes to apps, how it behaves is up to you. Whether an app continues to run after you have done something to it seems to be up to you. Adding or removing an app sounds pretty custom anyways.
Having said that, a time shouldn't be costing you anything anyways. The cost would come from what you do when the timer ticks. Disabling the timer should take care of that.
Aside from that, the mystery of what you are actually talking about dwarfs any response I can think of. What are these apps? Are they classes that can be disposed? Processes that can be suspended or terminated? Or something else entirely, such as programs launched via Process.Start?
My usual boring signature: Nothing
-
Oct 18th, 2012, 07:12 PM
#3
Re: Do timers continue to tick after an object instance is disposed?
What he is calling an app as he states is actually a custom control. @Op - Proceeding on that assumption, I'm informing you that simply removing a control from its container does not make it disappear. With no active references to it, any .Net object becomes eligible for garbage collection but this may not happen immediately so the object remains alive for an period of time. If one of you control's fields is a reference to a Timer and this Timer is still running, it would continue to do so even if you hold no references to your control after removing it from the container. But here's another kicker for you....Even if the garbage collector finally cleans up your control, remember the Timer itself is also a .Net object and its very possible that while it would be eligible for garbage collection, it may not be collected after your control is, thus it would still be executing.
The proper way to deal with this would be to override your custom control's Dispose method and in there you shut down the Timer and where every you are removing the control from the container by code, make sure to call Dispose. Please note though that controls sited on a Form using the VS designer are coded by the designer to dispose of all controls on its surface so you don't need to code this yourself if you place the control through the designer.
-
Oct 19th, 2012, 08:01 AM
#4
Thread Starter
Addicted Member
Re: Do timers continue to tick after an object instance is disposed?
To remove the control, I am simply using the Panel.Controls.Clear() function. Wouldn't use just a standalone Dispose() function dispose of more than just the individual control?
Always looking to meet fellow programmers. Message me if you want. Happy to help in whatever ways I can
-
Oct 19th, 2012, 08:03 AM
#5
Thread Starter
Addicted Member
Re: Do timers continue to tick after an object instance is disposed?
Also, by using a "Public Overrides" in the control's backing code, I'm not seeing an option for dispose. Am I doing it right?
Always looking to meet fellow programmers. Message me if you want. Happy to help in whatever ways I can
-
Oct 19th, 2012, 08:22 AM
#6
Re: Do timers continue to tick after an object instance is disposed?
Its not a public method. Its protected. Imagine the following is your control:-
vbnet Code:
Public Class MyControl
Inherits Control
Private WithEvents _tmr As Timer = New Timer
Public Sub New()
MyBase.Size = New Size(20, 20)
_tmr.Interval = 500
_tmr.Start()
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim rect As Rectangle = MyBase.ClientRectangle
rect.Inflate(-1, -1)
e.Graphics.DrawRectangle(Pens.Black, rect)
MyBase.OnPaint(e)
End Sub
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
_tmr.Stop()
_tmr.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
Private Sub _tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles _tmr.Tick
Static I As Integer = 1
I += 1
Debug.Print("Tick : " + I.ToString)
End Sub
End Class
You must override the protected Dispose method to disable the Timer. For the Timers to stop, you must dispose of the controls. Note that disposing of a control causes it to be removed from its container automatically. Remove them from your panel like:-
vbnet Code:
'
For Each C As MyControl In Panel1.Controls.OfType(Of MyControl)()
C.Dispose()
Next
Get it ?
-
Oct 19th, 2012, 09:13 AM
#7
Thread Starter
Addicted Member
Re: Do timers continue to tick after an object instance is disposed?
Thanks so much! Runs so much faster now!
Always looking to meet fellow programmers. Message me if you want. Happy to help in whatever ways I can
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|