VB.NET实现Singleton模式
Singleton模式:一个类只会创建一个实例。
下面的代码用VB.NET实现了Singleton模式:
下面的代码用VB.NET实现了Singleton模式:
Public Class Singleton
Private Shared _Singleton As Singleton
Private Shared _Mutex As New System.Threading.Mutex '同步基元也可用于进程间同步
Private Sub New()
End Sub
Public Shared Function GetInstance() As Singleton
_Mutex.WaitOne() '当在派生类中重写时,阻塞当前线程,直到当前的 System.Threading.WaitHandle 收到信号
Try
If _Singleton Is Nothing Then
_Singleton = New Singleton
End If
Finally
_Mutex.ReleaseMutex() '释放 System.Threading.Mutex 一次
End Try
Return _Singleton
End Function
End Class
Private Shared _Singleton As Singleton
Private Shared _Mutex As New System.Threading.Mutex '同步基元也可用于进程间同步
Private Sub New()
End Sub
Public Shared Function GetInstance() As Singleton
_Mutex.WaitOne() '当在派生类中重写时,阻塞当前线程,直到当前的 System.Threading.WaitHandle 收到信号
Try
If _Singleton Is Nothing Then
_Singleton = New Singleton
End If
Finally
_Mutex.ReleaseMutex() '释放 System.Threading.Mutex 一次
End Try
Return _Singleton
End Function
End Class