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
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