Mark Amans
Posts: 11
Joined: 2011-02-09
|
I created a shared memory object as a singleton using the advice I saw here: http://www.add-in-express.com/forum/read.php?FID=10&TID=9544
When I open a 2nd tab or window, a new instance of the singleton is being created. When I open a 3rd tab or window, it re-uses the singleton from the first tab. When I open a 4th tab it re-uses the singleton from the 2nd tab. I've tried this with up to 15 tabs open and it always follows the same pattern:
Singleton Instance 1: tab 1, tab 3, tab 5, tab 10
Singleton Instance 2: tab 2, tab 4, tab 8, tab 14
Singleton Instance 3: tab 6, tab 7, tab 9, tab 15
Singleton Instance 4: tab 11, tab 12, tab 13
I don't understand how the singleton is being instantiated more than once. Can anyone shed any light on this?
My singleton class is as follows:
Imports System
Imports System.Runtime.InteropServices
<ComVisible(False)> _
Public Class SharedData1
Private Shared _instance As SharedData1
Private Shared ReadOnly _lock As Object = New Object()
Private Shared _date As DateTime
Private Shared _string As String
Public Property DummyString() As String
Get
Return _string
End Get
Set(ByVal value As String)
_string = value
End Set
End Property
Protected Overrides Sub Finalize()
System.Diagnostics.Debug.WriteLine("Finalizing SharedData1: " & _date.Hour & ":" & _date.Minute & ":" & _date.Second & "." & _date.Millisecond)
End Sub
Private Sub New()
' Private constructor prevents instantiation outside the class
End Sub
Public Shared ReadOnly Property Instance() As SharedData1
Get
SyncLock (_lock)
If (SharedData1._instance Is Nothing) Then
SharedData1._instance = New SharedData1()
_date = DateTime.Now
System.Diagnostics.Debug.WriteLine("Creating new SharedData1: " & _date.Hour & ":" & _date.Minute & ":" & _date.Second & "." & _date.Millisecond)
Else
System.Diagnostics.Debug.WriteLine("SharedData1 already exists: " & _date.Hour & ":" & _date.Minute & ":" & _date.Second & "." & _date.Millisecond)
End If
End SyncLock
Return SharedData1._instance
End Get
End Property
End Class
I'm using ADXIE 2011 with Visual Studio 2010 |
|
Sergey Grischenko
Add-in Express team
Posts: 7233
Joined: 2004-07-05
|
Hi Mark,
Please open several IE tabs and then open the Task Manager. You will see several IE processes running at the same time. The fact is that IE opens tabs in different sub-processes and/or threads. That is why the shared memory object may be created more than once (it is typical for IE8 - IE10). |
|
Mark Amans
Posts: 11
Joined: 2011-02-09
|
Hi Sergey,
Then how can I share data between different instances of the toolbar if I can't use a single object like a singleton? Where should I store the data the I want to share?
Thanks,
Mark |
|
Sergey Grischenko
Add-in Express team
Posts: 7233
Joined: 2004-07-05
|
Hi Mark,
You can store data in a file as shown in the 'Synchronize settings' example. |
|
Sergey Grischenko
Add-in Express team
Posts: 7233
Joined: 2004-07-05
|
Mark,
you can also use the GetModuleByIndex, GetModuleByThread, GetModuleCount and GetModulesByTypeName (static) methods of the 'ADXIEModule' class to access your modules directly. |
|
Mark Amans
Posts: 11
Joined: 2011-02-09
|
I ended up using a MemoryMappedFile to share the data between the toolbar instances and it's working fine. |
|
Sergey Grischenko
Add-in Express team
Posts: 7233
Joined: 2004-07-05
|
Hi Mark,
Add-in Express provides the 'ADXIESharedMemory' class that implements file mapping. It is used in the 'Synchronize settings' example. |
|