Help disposing object

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

I'm having trouble understanding dispose. I set up a class that, among
other things, displays the time in a status bar panel. It does this by
starting a thread. I create an instance of this class on a form and when
the form closes, my thread continues to run.

Below is a simple custom control to include time in a status bar, followed
by a section of the form code that creates an instance of the control and
adds it to the form.

Could someone please tell me the proper way to set this up to dispose
properly when the user closes the form?

Thanks in advance for your help.

Dave

'//////////////////////////////////////////////////////////////////////////
'custom control test
'//////////////////////////////////////////////////////////////////////////
Imports System.Windows.Forms
Imports System.Threading

Public Class CustomStatus : Inherits System.Windows.Forms.StatusBar
Private tdPanel As New StatusBarPanel
Private tThread As Thread

Public Sub New()
MyBase.New()
tdPanel.AutoSize = StatusBarPanelAutoSize.Contents
MyBase.Panels.Add(tdPanel)
MyBase.ShowPanels = True
MyBase.Show()
Dim tThreadStart = New ThreadStart(AddressOf tUpdate)
tThread = New Thread(tThreadStart)
tThread.Start()
End Sub
Public Sub tUpdate()
Dim strDT As String
Do While True
strDT = " " & System.DateTime.Now.ToShortDateString
strDT = strDT & " " & System.DateTime.Now.ToLongTimeString
tdPanel.Text = strDT
tThread.Sleep(1000)
Loop
End Sub
Protected Overloads Sub dispose()
tThread.Abort()
MyBase.dispose()
End Sub
End Class

'//////////////////////////////////////////////////////////////////////////
'portion of form used for custom control test
'//////////////////////////////////////////////////////////////////////////
Private Sub frmConvert_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

pnl = New CustomStatus
Me.Controls.Add(pnl)

End Sub

Private Sub frmConvert_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing

pnl.dispose()

End Sub
 
You need to subscribe to the forms closing event. So on your control load,
use the FindForm function to get back a reference to the form. Then you can
hook the closing event and terminate your thread accordingly.

i.e.

private sub Control1_Load (sender as object, e as system.eventargs) handles
mybase.load

Dim tForm as Form
tForm = me.FindForm()

AddHandler tForm.Closing, addressof onParentFormClosing

end sub

Then in your closing event you can do likewise

private sub onParentFormClosing (sender as object, e as CancelEventArgs)

' stop your thread reference here...
...

'remove handlers...
dim tForm as Form
tForm = me.FindForm()
RemoveHandler tForm.Closing, AddressOf onParentFormClosing
tForm = nothing
end sub
 
Thank you CJ.

Dave

CJ Taylor said:
You need to subscribe to the forms closing event. So on your control load,
use the FindForm function to get back a reference to the form. Then you can
hook the closing event and terminate your thread accordingly.

i.e.

private sub Control1_Load (sender as object, e as system.eventargs) handles
mybase.load

Dim tForm as Form
tForm = me.FindForm()

AddHandler tForm.Closing, addressof onParentFormClosing

end sub

Then in your closing event you can do likewise

private sub onParentFormClosing (sender as object, e as CancelEventArgs)

' stop your thread reference here...
...

'remove handlers...
dim tForm as Form
tForm = me.FindForm()
RemoveHandler tForm.Closing, AddressOf onParentFormClosing
tForm = nothing
end sub


'//////////////////////////////////////////////////////////////////////////
'//////////////////////////////////////////////////////////////////////////
'//////////////////////////////////////////////////////////////////////////
'//////////////////////////////////////////////////////////////////////////
 
Back
Top