PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 Do timers continue to tick after an object instance is disposed?-VBForums
Results 1 to 7 of 7

Thread: Do timers continue to tick after an object instance is disposed?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Massachusetts
    Posts
    205

    Question 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

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,297

    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

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,639

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Massachusetts
    Posts
    205

    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Massachusetts
    Posts
    205

    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

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,639

    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:
    1. Public Class MyControl
    2.     Inherits Control
    3.  
    4.     Private WithEvents _tmr As Timer = New Timer
    5.  
    6.     Public Sub New()
    7.  
    8.         MyBase.Size = New Size(20, 20)
    9.  
    10.         _tmr.Interval = 500
    11.         _tmr.Start()
    12.     End Sub
    13.  
    14.     Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    15.  
    16.         Dim rect As Rectangle = MyBase.ClientRectangle
    17.  
    18.         rect.Inflate(-1, -1)
    19.  
    20.         e.Graphics.DrawRectangle(Pens.Black, rect)
    21.  
    22.         MyBase.OnPaint(e)
    23.     End Sub
    24.  
    25.     Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    26.  
    27.         If disposing Then
    28.             _tmr.Stop()
    29.             _tmr.Dispose()
    30.         End If
    31.  
    32.         MyBase.Dispose(disposing)
    33.     End Sub
    34.  
    35.     Private Sub _tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles _tmr.Tick
    36.         Static I As Integer = 1
    37.  
    38.         I += 1
    39.         Debug.Print("Tick : " + I.ToString)
    40.     End Sub
    41. 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:
    1. '
    2.         For Each C As MyControl In Panel1.Controls.OfType(Of MyControl)()
    3.             C.Dispose()
    4.         Next

    Get it ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Location
    Massachusetts
    Posts
    205

    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
  •  



Featured


Click Here to Expand Forum to Full Width