|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThere may be times when you want to be sure that there is only one instance of a form within your Compact Framework application. You may also need to be able to pass data between open forms. This is why we use the singleton design pattern. You may have used this in a standard Windows application, but a Compact Framework application is a bit trickier. Preparing the FormThe first thing that you will need to do is add an explicit implementation of the Public Class MyForm
Inherits System.Windows.Forms.Form
Implements IDisposable
....
Next, you will want to change the constructor of the form to Private Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
Go ahead and remove the Next, you'll need to add two internal members: Private handle As IntPtr
Private Shadows disposed As Boolean = False
*You have to declare the disposed member as FinalizationAdd the necessary finalization methods: #Region " Finalization "
Public Overloads Sub Dispose() Implements
IDisposable.Dispose
Dispose(True)
' Take yourself off of the finalization queue
' to prevent finalization code for this object
' from executing a second time.
GC.SuppressFinalize(Me)
End Sub
Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
' Check to see if Dispose has already been called.
If Not (Me.Disposed) Then
' If disposing equals true, dispose all managed
' and unmanaged resources.
If (disposing) Then
' Dispose managed resources.
End If
' Release unmanaged resources. If disposing is false,
' only the following code is executed.
handle = IntPtr.Zero
' Note that this is not thread safe.
' Another thread could start disposing the object
' after the managed resources are disposed,
' but before the disposed flag is set to true.
' If thread safety is necessary, it must be
' implemented by the client.
End If
Me.Disposed = True
End Sub
' This Finalize method will run only if the
' Dispose method does not get called.
' By default, methods are NotOverridable.
' This prevents a derived class from overriding this method.
Protected Overrides Sub Finalize()
' Do not re-create Dispose clean-up code here.
' Calling Dispose(false) is optimal in terms of
' readability and maintainability.
Dispose(False)
End Sub
'You'll need to handle the Closed event to ensure that the
'form is disposed when closed.
Private Sub MyForm_Closed(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Closed
Me.Dispose()
End Sub
#End Region
Shared InstanceNow, the last part. You'll need to add an ...
End Sub
Private Shared _Instance As MyForm = Nothing
Public Shared Function Instance() As MyForm
If _Instance Is Nothing OrElse _Instance.disposed = True Then
_Instance = New MyForm
End If
_Instance.BringToFront()
Return _Instance
End Function
|
|||||||||||||||||||||||||||||||||||