Single instance of an object between different processes?

  • Thread starter Thread starter Guest
  • Start date Start date
Hi,

You can create a (static) class and expose the object as a static member of
this class:

public static class Globals // 'static' here only valid in C# 2.0
{
// the "global" instance
private static TheClass theObject;
// a public property that exposes the instance
public static TheClass TheObject { get { return theObject; } }
// class constructor - executed when class is loaded
Globals()
{
// create and initialize "global" instance
theObject = new TheClass();
// etc.
}
}

Applications can reference this object by Globals.TheObject.

Regards - Octavio
 
You can also use remoting here, and use the object in the singletone
instantiation pattern
 
Hi,

I have an application that is made up of several executables. I need all
these executables to use the same instance of an object.

What is the best, most efficient way to approach this?

Thanks a lot!
 
Elad said:
I wish to use a single instance of an object across different AppDomains. I
do not need to verify whether an EXE application already has an instance
running. The singleton pattern does not apply for multiple AppDomains,
since it is an in-process pattern.

I believe my only choices are Remoting and WebServices. Am I wrong?

I don't think web services are going to be a good way for you to go, if
I understand your situation correctly. Remoting will probably be a good
idea. There's a good article by Ingo Rammer at
http://www.thinktecture.com/Resources/RemotingFAQ/RemotingUseCases.html
which compares different Remoting use cases, and there are also hints at
competing technologies if you should find that Remoting is not right for
you after all.



Oliver Sturm
 
Hi all, thanks for your replies.

I may have done a poor job of explaining myself.

I wish to use a single instance of an object across different AppDomains. I
do not need to verify whether an EXE application already has an instance
running. The singleton pattern does not apply for multiple AppDomains,
since it is an in-process pattern.

I believe my only choices are Remoting and WebServices. Am I wrong?

Thanks again.
 
You could use remoting or use a Singleton class that derives from
MarshalByRef and then you could keep that in a named appdomain. You then just
marshal the singleton from that appdomain into the one you are using and then
access the underlying object.


--
Brian Delahunty
Ireland

http://briandela.com/blog

INDA SouthEast - http://southeast.developers.ie/ - The .NET usergroup I
started in the southeast of Ireland.
 
Brian, thanks for your help...

Maybe I'm missing something: How can mutexes help me if I require a single
instance of a generic class (not a single instance of a process) throughout
a single machine...
 
Don't worry about it - I'm like that everyday! And anyway, since most of
the people 'round here gave me answers to different questions, it's more
probable that I did not clarify myself.

Is there a way of achieving the Singleton you mentioned without involving
..Net Remoting? MSDN states that MarshalByRef is used for objects that are
meant to support Remoting.

I simple need one instance of an object to be used by several applications.
With COM this was simple enough.....
 
You don't have to use remoting. MarshalByRef just allows you to marshal
across app boundaries (including appdomains). You don't need to use remoting
at all.

For example:

// Your Singleton
private class ExampleSingletonClass: System.MarshalByRefObject
{
// Singleton implementation
}

The singleton will be created in a named appdomain... for example:


// Create a temporary application domain to load the singleton into
AppDomain singletonDomain = AppDomain.CreateDomain("SingletonDomain");

ExampleSingletonClass singleton =
singletonDomain.CreateInstanceFromAndUnwrap(Assembly.GetExecutingAssembly().Location, typeof(ExampleSingletonClass).FullName) as ExampleSingletonClass;

// Do whatever you want with the singleton

// At the end of your app, kill the singleton domain
AppDomain.Unload(singletonDomain);



Hope this helps.

--
Brian Delahunty
Ireland

http://briandela.com/blog

INDA SouthEast - http://southeast.developers.ie/ - The .NET usergroup I
started in the southeast of Ireland.
 
Brian,

Thanks a lot for the detailed example. I'll check it out and see if it
works for me.

Thanks again.

Elad
 
Brian,

Assuming I have two separate EXE applications that are meant to use the same
SingletonClass object that resides in Singleton.dll, how does the
SingletonClass object get instantiated? I need both EXE applications to use
the same instance of SingletonClass, the declaration of which resides in the
DLL. I don't exactly understand how this is accomplished using the code you
provided.

Basically, what I'm trying to accomplish is this: I have an object that
represents a sort of repository (for the sake of argument, a hierarchy of
folders and elements within the folders). I have two different client
applications that use this repository at the same time. I want to keep them
synchronized, so if one application changes the folder hierarchy, the other
application would be immediately notified so it can refresh the visual
representation of the folder hierarchy. Synchronization is also critical to
achieve some other goals which I won't bore you with.

Sorry for being difficult!

Elad
 
Essentially, you will need to host your singleton in either a stand-alone
exe, or a service. I would definately recommend remoting, as it is very
simple to use. If you use a stand-alone exe, you will obviously need to make
sure it is available when needed, and ensure only one instance of the exe is
running at any one time (using a mutex).

If you do not want to use remoting, you can use any number of other
interprocess communication protocols. Remoting in this instance is the most
suitable, IMHO.

HTH
Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top