16,721
社区成员




Public Class Form1
Public bb As New SortedList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim class1 As New Class1("第1个类")
bb.Add(0, class1)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim class1 As New Class1("第2个类")
Dim t As Class1 = bb.Item(0)
't.Dispose()
't = Nothing
'bb.Remove(0)
bb.Add(0, class1)
End Sub
End Class
Public Class Class1
Public aa As String
Public WithEvents t As New Timer
Sub New(ByVal value As String)
aa = value
t.Interval = 1000
t.Start()
End Sub
Private Sub t_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles t.Tick
Debug.WriteLine(Now.ToString & "---->" & aa)
End Sub
End Class
Public Class Class1
Implements IDisposable
Protected disposed As Boolean = False
Public aa As String
Public WithEvents t As New Timer
Sub New(ByVal value As String)
aa = value
t.Interval = 1000
t.Start()
End Sub
Private Sub t_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles t.Tick
Debug.WriteLine(Now.ToString & "---->" & aa)
End Sub
Protected Overridable Overloads Sub Dispose( _
ByVal disposing As Boolean)
If Not Me.disposed Then
If disposing Then
t.Dispose() '时钟对象是销毁了,但是这个类的实例也同时销毁了吗?
End If
End If
Me.disposed = True
End Sub
Public Sub AnyOtherMethods()
If Me.disposed Then
Throw New ObjectDisposedException(Me.GetType().ToString, "This object has been disposed.")
End If
End Sub
#Region " IDisposable Support "
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
MyBase.Finalize()
End Sub
#End Region
End Class