I have a VB.net dll project with a class that is a singleton. I've been
using this in winform apps without any problems. I would like to use this
same dll in a web form project but my singleton will cause problems because
some sessions may need different values in the singleton. I want to change
my singleton to store a private hashtable with different instances. I guess
this is more like a factory pattern now but that doesn't matter My main
concern is about thread safty. The code below is what I have so far. Is
this the correct way to protect access to the inner hashtable? Thanks for
the comments or suggestions.
Eric
Friend NotInheritable Class RuleManager
Private Shared mInnerList As new HashTable
Private Sub New()
'hide constructor
End Sub
Public Shared ReadOnly Property Instance(Optional ByVal UniqueKey As
String = "") As RuleManager
Get
If mInnerList.Contains(UniqueKey) Then
Return DirectCast(mInnerList(UniqueKey), RuleManager)
End If
SyncLock mInnerList.SyncRoot
If Not mInnerList.Contains(UniqueKey) Then
mInnerList(UniqueKey) = New RuleManager
End If
Return DirectCast(mInnerList(UniqueKey), RuleManager)
End SyncLock
End Get
End Property
End class 11 4882
Eric,
Have you considered something along these lines for your instance
property? (Assume Imports System.Web.HttpContext, and _Instance is a
private member declaration of your class.)
Public Shared ReadOnly Property Instance() As RuleManager
'// Check to see if a session variable with the signature of
the
'// current singleton object exists.
If Current.Session("RuleManager") Is Nothing Then
'// Create a new instance of the object.
_Instance = New RuleManager
'// Store the object in the session, using the signature
declared
'// to retrieve it when requested.
Current.Session("RuleManager") = _Instance
Else
'// Retrieve the instance that currently exists in the
session.
_Instance = CType(Current.Session("RuleManager"),
RuleManager)
End If
'// Return the singular session instance of the singleton,
whether
'// it was created or retrieved from the session.
Return _Instance
End Function
You are basically creating a Singleton for each individual user and
storing it in their Session. You don't have to worry about
cross-polluting the session now.
HTH,
Joseph
Joseph,
Thanks for your suggestion. I would use that approach but the dll that I
currently have has the RuleManager scoped as Friend. This class is internal
to my dll. Basically all of my classes in the dll need to be able to work
with this RuleManager but the RuleManager may need to be different for each
session. Remember this dll needs to work with both web apps and winforms.
Thanks,
Eric
"Joseph Ferris" <jo***********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com... Eric,
Have you considered something along these lines for your instance property? (Assume Imports System.Web.HttpContext, and _Instance is a private member declaration of your class.)
Public Shared ReadOnly Property Instance() As RuleManager
'// Check to see if a session variable with the signature of the '// current singleton object exists. If Current.Session("RuleManager") Is Nothing Then
'// Create a new instance of the object. _Instance = New RuleManager
'// Store the object in the session, using the signature declared '// to retrieve it when requested. Current.Session("RuleManager") = _Instance
Else
'// Retrieve the instance that currently exists in the session. _Instance = CType(Current.Session("RuleManager"), RuleManager)
End If
'// Return the singular session instance of the singleton, whether '// it was created or retrieved from the session. Return _Instance
End Function
You are basically creating a Singleton for each individual user and storing it in their Session. You don't have to worry about cross-polluting the session now.
HTH,
Joseph
Eric,
Gotcha. Sorry I couldn't be of further assistance.
Joseph
Eric,
It is thread-safe, but only because the Hashtable is unique in that it
can safely support one writer and multiple readers simultaneously.
Brian
Eric wrote: I have a VB.net dll project with a class that is a singleton. I've been using this in winform apps without any problems. I would like to use this same dll in a web form project but my singleton will cause problems because some sessions may need different values in the singleton. I want to change my singleton to store a private hashtable with different instances. I guess this is more like a factory pattern now but that doesn't matter My main concern is about thread safty. The code below is what I have so far. Is this the correct way to protect access to the inner hashtable? Thanks for the comments or suggestions.
Eric
Friend NotInheritable Class RuleManager Private Shared mInnerList As new HashTable Private Sub New() 'hide constructor End Sub
Public Shared ReadOnly Property Instance(Optional ByVal UniqueKey As String = "") As RuleManager Get If mInnerList.Contains(UniqueKey) Then Return DirectCast(mInnerList(UniqueKey), RuleManager) End If
SyncLock mInnerList.SyncRoot If Not mInnerList.Contains(UniqueKey) Then mInnerList(UniqueKey) = New RuleManager End If Return DirectCast(mInnerList(UniqueKey), RuleManager) End SyncLock
End Get End Property End class
If it's a singleton, won't all apps using the dll share the same hashtable
since there is only one instance of the class? Also, I thought you had to
use synclock for reads as well as writes for threadsafe classes. Sorry, I'm
just learning!
--
Dennis in Houston
"Eric" wrote: Joseph,
Thanks for your suggestion. I would use that approach but the dll that I currently have has the RuleManager scoped as Friend. This class is internal to my dll. Basically all of my classes in the dll need to be able to work with this RuleManager but the RuleManager may need to be different for each session. Remember this dll needs to work with both web apps and winforms.
Thanks,
Eric
"Joseph Ferris" <jo***********@gmail.com> wrote in message news:11**********************@g44g2000cwa.googlegr oups.com... Eric,
Have you considered something along these lines for your instance property? (Assume Imports System.Web.HttpContext, and _Instance is a private member declaration of your class.)
Public Shared ReadOnly Property Instance() As RuleManager
'// Check to see if a session variable with the signature of the '// current singleton object exists. If Current.Session("RuleManager") Is Nothing Then
'// Create a new instance of the object. _Instance = New RuleManager
'// Store the object in the session, using the signature declared '// to retrieve it when requested. Current.Session("RuleManager") = _Instance
Else
'// Retrieve the instance that currently exists in the session. _Instance = CType(Current.Session("RuleManager"), RuleManager)
End If
'// Return the singular session instance of the singleton, whether '// it was created or retrieved from the session. Return _Instance
End Function
You are basically creating a Singleton for each individual user and storing it in their Session. You don't have to worry about cross-polluting the session now.
HTH,
Joseph
Dennis,
My understanding is that the dll will get loaded once for each win app. I
beleive there would be a new singleton for each of this apps becuase of
that. As for an asp.net app, I'm not sure excatly how many times the dll
would get loaded. I'm assuming the dll would only get loaded once. This is
the situation where I would need to make use of the hashtable so that each
session could get a different instance of the singleton.
Thanks,
Eric
"Dennis" <De****@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com... If it's a singleton, won't all apps using the dll share the same hashtable since there is only one instance of the class? Also, I thought you had to use synclock for reads as well as writes for threadsafe classes. Sorry, I'm just learning! -- Dennis in Houston
"Eric" wrote:
Joseph,
Thanks for your suggestion. I would use that approach but the dll that I currently have has the RuleManager scoped as Friend. This class is internal to my dll. Basically all of my classes in the dll need to be able to work with this RuleManager but the RuleManager may need to be different for each session. Remember this dll needs to work with both web apps and winforms.
Thanks,
Eric
"Joseph Ferris" <jo***********@gmail.com> wrote in message news:11**********************@g44g2000cwa.googlegr oups.com... > Eric, > > Have you considered something along these lines for your instance > property? (Assume Imports System.Web.HttpContext, and _Instance is a > private member declaration of your class.) > > Public Shared ReadOnly Property Instance() As RuleManager > > '// Check to see if a session variable with the signature of > the > '// current singleton object exists. > If Current.Session("RuleManager") Is Nothing Then > > '// Create a new instance of the object. > _Instance = New RuleManager > > '// Store the object in the session, using the signature > declared > '// to retrieve it when requested. > Current.Session("RuleManager") = _Instance > > Else > > '// Retrieve the instance that currently exists in the > session. > _Instance = CType(Current.Session("RuleManager"), > RuleManager) > > End If > > '// Return the singular session instance of the singleton, > whether > '// it was created or retrieved from the session. > Return _Instance > > End Function > > You are basically creating a Singleton for each individual user and > storing it in their Session. You don't have to worry about > cross-polluting the session now. > > HTH, > > Joseph >
Dennis,
No, applications do not share objects or any other data from the dll.
Each application will execute the code in the dll independently of the
others so they will all be working with different instances of the
hashtable.
Yes, generally speaking you do have to use a lock for both reads and
writes for the code to be thread-safe. There are exceptions though.
In this particular case the code is thread-safe because 1) the
Hashtable can support one writer and multiple readers concurrently and
2) items are never removed from the Hashtable. The Hashtable is the
only collection in the .NET Framework that has partial thread-safety.
In general, I would avoid the practice of attempting to access data
structures without locks in this manner. Subtle problems might crop up
when the code is ran on different platforms or framework versions.
Brian
Dennis wrote: If it's a singleton, won't all apps using the dll share the same hashtable since there is only one instance of the class? Also, I thought you had to use synclock for reads as well as writes for threadsafe classes. Sorry, I'm just learning! -- Dennis in Houston
Brian,
I didn't really think about it yet until your message but I will have to
add a method to remove from the hashtable at some point. Do you have any
examples of a thread safe way to read and write to any colletcion? I may
want to change the hashtable implementation at some point.
Thanks,
Eric
"Brian Gideon" <br*********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... Dennis,
No, applications do not share objects or any other data from the dll. Each application will execute the code in the dll independently of the others so they will all be working with different instances of the hashtable.
Yes, generally speaking you do have to use a lock for both reads and writes for the code to be thread-safe. There are exceptions though. In this particular case the code is thread-safe because 1) the Hashtable can support one writer and multiple readers concurrently and 2) items are never removed from the Hashtable. The Hashtable is the only collection in the .NET Framework that has partial thread-safety.
In general, I would avoid the practice of attempting to access data structures without locks in this manner. Subtle problems might crop up when the code is ran on different platforms or framework versions.
Brian
Dennis wrote: If it's a singleton, won't all apps using the dll share the same hashtable since there is only one instance of the class? Also, I thought you had to use synclock for reads as well as writes for threadsafe classes. Sorry, I'm just learning! -- Dennis in Houston
Eric,
Change the Instance property so that it acquires a lock everytime like
the following.
Public Shared ReadOnly Property Instance( _
Optional ByVal UniqueKey As String = "") As RuleManager
Get
SyncLock mInnerList.SyncRoot
If Not mInnerList.Contains(UniqueKey) Then
mInnerList(UniqueKey) = New RuleManager
End If
Return DirectCast(mInnerList(UniqueKey), RuleManager)
End SyncLock
End Get
End Property
Also, wrap a SyncLock around everything in the method that will remove
from the collection as well.
Brian
Eric wrote: Brian,
I didn't really think about it yet until your message but I will have to add a method to remove from the hashtable at some point. Do you have any examples of a thread safe way to read and write to any colletcion? I may want to change the hashtable implementation at some point.
Thanks,
Eric
Thanks for taking the time to explain the singleton.
--
Dennis in Houston
"Eric" wrote: Brian,
I didn't really think about it yet until your message but I will have to add a method to remove from the hashtable at some point. Do you have any examples of a thread safe way to read and write to any colletcion? I may want to change the hashtable implementation at some point.
Thanks,
Eric "Brian Gideon" <br*********@yahoo.com> wrote in message news:11**********************@z14g2000cwz.googlegr oups.com... Dennis,
No, applications do not share objects or any other data from the dll. Each application will execute the code in the dll independently of the others so they will all be working with different instances of the hashtable.
Yes, generally speaking you do have to use a lock for both reads and writes for the code to be thread-safe. There are exceptions though. In this particular case the code is thread-safe because 1) the Hashtable can support one writer and multiple readers concurrently and 2) items are never removed from the Hashtable. The Hashtable is the only collection in the .NET Framework that has partial thread-safety.
In general, I would avoid the practice of attempting to access data structures without locks in this manner. Subtle problems might crop up when the code is ran on different platforms or framework versions.
Brian
Dennis wrote: If it's a singleton, won't all apps using the dll share the same hashtable since there is only one instance of the class? Also, I thought you had to use synclock for reads as well as writes for threadsafe classes. Sorry, I'm just learning! -- Dennis in Houston
, I'm not sure excatly how many times the dll would get loaded. I'm assuming the dll would only get loaded once
Be aware that there can be multiple ASP.Net Worker threads starting your app
at the same time , on my webserver the webserver starts my projects 4 times
( i receive a e-mail when my singleton is created ) so your assumination
that it might get started once can be wrong .
regards
Michel Posseth [MCP]
"Eric" <an******@usa.com> wrote in message
news:eg**************@tk2msftngp13.phx.gbl... Dennis,
My understanding is that the dll will get loaded once for each win app. I beleive there would be a new singleton for each of this apps becuase of that. As for an asp.net app, I'm not sure excatly how many times the dll would get loaded. I'm assuming the dll would only get loaded once. This is the situation where I would need to make use of the hashtable so that each session could get a different instance of the singleton.
Thanks,
Eric "Dennis" <De****@discussions.microsoft.com> wrote in message news:87**********************************@microsof t.com... If it's a singleton, won't all apps using the dll share the same hashtable since there is only one instance of the class? Also, I thought you had to use synclock for reads as well as writes for threadsafe classes. Sorry, I'm just learning! -- Dennis in Houston
"Eric" wrote:
Joseph,
Thanks for your suggestion. I would use that approach but the dll that I currently have has the RuleManager scoped as Friend. This class is internal to my dll. Basically all of my classes in the dll need to be able to work with this RuleManager but the RuleManager may need to be different for each session. Remember this dll needs to work with both web apps and winforms.
Thanks,
Eric
"Joseph Ferris" <jo***********@gmail.com> wrote in message news:11**********************@g44g2000cwa.googlegr oups.com... > Eric, > > Have you considered something along these lines for your instance > property? (Assume Imports System.Web.HttpContext, and _Instance is a > private member declaration of your class.) > > Public Shared ReadOnly Property Instance() As RuleManager > > '// Check to see if a session variable with the signature of > the > '// current singleton object exists. > If Current.Session("RuleManager") Is Nothing Then > > '// Create a new instance of the object. > _Instance = New RuleManager > > '// Store the object in the session, using the signature > declared > '// to retrieve it when requested. > Current.Session("RuleManager") = _Instance > > Else > > '// Retrieve the instance that currently exists in the > session. > _Instance = CType(Current.Session("RuleManager"), > RuleManager) > > End If > > '// Return the singular session instance of the singleton, > whether > '// it was created or retrieved from the session. > Return _Instance > > End Function > > You are basically creating a Singleton for each individual user and > storing it in their Session. You don't have to worry about > cross-polluting the session now. > > HTH, > > Joseph >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: David |
last post by:
Hi guys
I have in my application many collections (HashTable and
ArrayList)loaded with configuration data, that means they are loaded
at startup and then they are accessed only for read-only...
|
by: Support |
last post by:
This doubt is regarding synchronisation question in Singleton pattern code
of C#
I had created a class as
public sealed class SecuriteManager
{
private static volatile SecurityManager...
|
by: Robert Zurer |
last post by:
I have a server application that makes a MarshalByReferenceObject available
via remoting. It's lifetime is set to never expire and it is implemented as
a Singleton.
Are calls to this object...
|
by: Mountain Bikn' Guy |
last post by:
Is the second version shown below better? I couldn't locate enough info
about in order to tell.
1.. My commonly used singleton pattern implementation looks like this (it
was inspired by Eric...
|
by: Diffident |
last post by:
Guys,
I have been cracking my head over this concept in .NET framework. I have
read many posts on this topic but not clear about this and hence I am posting
it again.
If you have designed...
|
by: William Sullivan |
last post by:
I'm trying to nail down some issues with the cache in my application.
Currently, I have an object that stands between my business logic and
database logic called CacheLogic (cute, no?). ...
|
by: Chad Zalkin |
last post by:
We are evaluating some old code that was written as part of our math
library.
This code uses some optimizations that I'm not sure are necessary or
safe, but is a source of debate between my...
|
by: intrader |
last post by:
I have the following small classes:
//----------------code---------------
using System;
using System.Collections.Generic;
using System.Text;
namespace ValidatorsLibrary
{
public class...
|
by: Angus |
last post by:
Hello
I have written a singleton class like this:
//---------------------------------------------------------------------------
// Instance...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |