本文整理匯總了VB.NET中System.WeakReference類的典型用法代碼示例。如果您正苦於以下問題:VB.NET WeakReference類的具體用法?VB.NET WeakReference怎麽用?VB.NET WeakReference使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了WeakReference類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Example
' 導入命名空間
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
' Create the cache.
Dim cacheSize As Integer = 50
Dim r As Random = New Random()
Dim c As Cache = New Cache(cacheSize)
Dim DataName As String = ""
GC.Collect(0)
' Randomly access objects in the cache.
For ctr As Integer = 0 To C.Count - 1
Dim index As Integer = r.Next(c.Count)
' Access the object by getting a property value.
DataName = c(index).Name
Next
' Show results.
Dim regenPercent As Double = c.RegenerationCount * 100 / c.Count
Console.WriteLine("Cache size: {0}, Regenerated: {1}%", c.Count, regenPercent)
End Sub
End Class
Public Class Cache
' Dictionary to contain the cache.
Private Shared _cache As Dictionary(Of Integer, WeakReference)
' Track the number of times an object is regenerated.
Dim regenCount As Integer = 0
Public Sub New(ByVal count As Integer)
_cache = New Dictionary(Of Integer, WeakReference)
' Add data objects with a short weak reference to the cache.
For ctr = 0 To count - 1
_cache.Add(ctr, New WeakReference(New Data(ctr)))
Next
End Sub
' Number of items in the cache.
Public ReadOnly Property Count() As Integer
Get
Return _cache.Count
End Get
End Property
' Number of times an object needs to be regenerated.
Public ReadOnly Property RegenerationCount() As Integer
Get
Return regenCount
End Get
End Property
' Retrieve a data object from the cache.
Default Public ReadOnly Property Item(ByVal index As Integer) As Data
Get
Dim d As Data = TryCast(_cache(index).Target, Data)
' If the object was reclaimed, generate a new one.
If d Is Nothing Then
Console.WriteLine("Regenerate object at {0}: Yes", index)
d = New Data(index)
_cache(index).Target = d
regenCount += 1
Else
' Object was obtained with the weak reference.
Console.WriteLine("Regenerate object at {0}: No", index.ToString())
End If
Return d
End Get
End Property
End Class
' Class that creates byte arrays to simulate data.
Public Class Data
Private _data() As Byte
Private _name As String
Public Sub New(ByVal size As Integer)
_data = New Byte(((size * 1024)) - 1) {}
_name = size.ToString
End Sub
' Simple property for accessing the object.
Public ReadOnly Property Name() As String
Get
Return _name
End Get
End Property
End Class
' Example of the last lines of the output:
' ...
' Regenerate object at 36: Yes
' Regenerate object at 8: Yes
' Regenerate object at 21: Yes
' Regenerate object at 4: Yes
' Regenerate object at 38: No
' Regenerate object at 7: Yes
' Regenerate object at 2: Yes
' Regenerate object at 43: Yes
' Regenerate object at 38: No
' Cache size: 50, Regenerated: 94%
示例2: New WeakReference
public class Test
public Shared Sub Main
Dim wr As WeakReference
Dim obj As New Person()
wr = New WeakReference(obj)
End Sub
End class
Public Class Person
Implements IDisposable
Private Disposed As Boolean = False
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
System.GC.SuppressFinalize(Me)
End Sub
Protected Overloads Sub Dispose(ByVal disposing As Boolean)
Me.Disposed = True
End Sub
Protected Overrides Sub finalize()
Console.WriteLine("In Person Finalize")
MyBase.Finalize()
Me.Dispose(False)
End Sub
End Class