
singleton class please help
Well, if what you want is only keep one object instance system wide, it's
not hard.
You could have a system wide symbol to identify whether the "one-and-only"
object has been created.
For example, you could use Mutex with following ctor and set name to
something special (say, a GUID):
public Mutex(
bool initiallyOwned,
string name,
out bool createdNew
);
The Application which gets createdNew equals true could go ahead and create
the object.
For others, the object already exists.
But after that, the real problem comes: how can you use the singleton object
across process/appdomain boundary?
As I can see, remoting is the best solution. For the application which
creates the object, you could also make it running as a remoting server. So
that other applications could retrive the object through a remoting channel.
Hope this helps.
Ming Chen [MVP]
Quote:
> hi
> thank you very much. Yes i think im trying to access them from two
different
> process. That is why im getting two instances. Could you explain me
further
> on how to make it available for process wide with the same instance. Do i
> just have to access the class thro remoting? or do i need to do anything
> extra. or is there any example available?
> thanks
> naveen
> > Hi, Naveen.
> > As a pattern, Singleton only works within address space boundary of an
> > application. For win32, it's Process. While for .NET application, it's
> > AppDomain.
> > To make it work system wide, you have to add some other mechanism. For
> > example, you could use .Net Remoting: Start an object server which
> provides
> > the singleton instance, and all other applications access the object
> through
> > remoting.
> > Hope this helps.
> > Ming Chen [MVP]
> > > Hi
> > > I am trying to create a singleton class. I have a static member
variable
> > > that stores the same class instance. I want to create it only once.
But
> > > somehow it is not happeneing and it is creating a new instance for
each
> > > application that i try to access. Pl find below my code.
> > > if anyone can throw any idea on this, it will be of great help.
> > > thanks
> > > naveen
> > > sealed public class CSingletonClass
> > > {
> > > public static CSingletonClass Instance= null;
> > > private string m_ConnectionString =null;
> > > public CSingletonClass()
> > > {
> > > Debug.WriteLine("Constructor called");
> > > }
> > > public static CSingletonClass GetInstance()
> > > {
> > > if(Instance==null)
> > > {
> > > Debug.WriteLine("Create a new instance");
> > > Instance = new CSingletonClass();
> > > return Instance;
> > > }
> > > else
> > > {
> > > return Instance;
> > > }
> > > }
> > > public string GetConnectionString()
> > > {
> > > if(m_ConnectionString==null)
> > > {
> > > //get the connection string form the database..
> > > Debug.WriteLine("getting the connection string from the database");
> > > m_ConnectionString = "New actual connection string";
> > > return m_ConnectionString;
> > > }
> > > else
> > > {
> > > return m_ConnectionString;
> > > }
> > > }
> > > public string ConnectionString
> > > {
> > > get
> > > {
> > > Debug.WriteLine("Returning the connection string");
> > > return m_ConnectionString;
> > > }
> > > set
> > > {
> > > Debug.WriteLine("Getting the connection string");
> > > m_ConnectionString = value;
> > > }
> > > }