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 2 1218
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
"Dave" <vb_re_move_@4scotts_re_move_.com> wrote in message
news:un**************@TK2MSFTNGP09.phx.gbl... 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
Thank you CJ.
Dave
"CJ Taylor" <no****@blowgoats.com> wrote in message
news:10*************@corp.supernews.com... 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
"Dave" <vb_re_move_@4scotts_re_move_.com> wrote in message news:un**************@TK2MSFTNGP09.phx.gbl... 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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mike |
last post by:
Please help this is driving me nuts. I have 2 forms, 1 user class and I am
trying to implement a singleton class. Form 1 should create a user object
and populate some properties in user. Form2...
|
by: MyNameIsnt |
last post by:
Can anyone tell me why, when I click on the buttons it register 2
characters on the display?
if you use the right mousebutton it works ok, but the buttons dont
flash??
it works fine without the...
|
by: MFC |
last post by:
Ok, after three C# books, (C# How to Program, Programming in the Key
of C#, and C# Weekend Crash Course) and three weeks, I believe I have
tried everything to make a certain form function...
|
by: Popoxinhxan |
last post by:
Dear experts, i want to develop an client application that consume the google
search web service.
In my MainForm i have a method to retrieve all the search result e.g.
GetGoogleResults().
Now i...
|
by: a_deano |
last post by:
Hi,
I am not totally new to programming for Windows but may have bitten off
more than I can chew.
My problem is, I would like to install a hook (using SetWindowsHookEx)
to monitor mouse...
|
by: Juan Romero |
last post by:
Hey guys, please HELP
I am going nuts with the datagrid control.
I cannot get the damn control to refresh. I am using soap to get information
from a web service. I have an XML writer output...
|
by: Brian |
last post by:
I am very new to VB.NET and I just need some help in a listbox matter. I
don't need suggestions on how to make the overall code better. I will get
there.
I want to move an item from one list box...
|
by: Edwinah63 |
last post by:
Hi everyone,
i was wondering if anyone else had observed this and if there is any
workaround or patch.
i have some screens (an mdi child forms) with the buttons &Add, &Edit,
&Save and E&xit,...
|
by: cj |
last post by:
Lets just take this example I'm looking at now. I'm looking at the help
screen titled .NET Framework Class Library FolderBrowserDialog Class
. It gives an example at the bottom that begins with:...
|
by: =?Utf-8?B?U2l2?= |
last post by:
I have a form that I programmatically generate some check boxes and labels on.
Later on when I want to draw the form with different data I want to clear
the previously created items and then put...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |