Click here to Skip to main content
15,902,938 members
Home / Discussions / C#
   

C#

 
GeneralRe: Dir Search On Server Pin
Heath Stewart20-May-04 4:43
protectorHeath Stewart20-May-04 4:43 
GeneralProblems with Screen.PrimaryScreen.Bounds Pin
kaloyan20-May-04 3:21
kaloyan20-May-04 3:21 
GeneralRe: Problems with Screen.PrimaryScreen.Bounds Pin
Heath Stewart20-May-04 4:14
protectorHeath Stewart20-May-04 4:14 
GeneralRe: Problems with Screen.PrimaryScreen.Bounds Pin
kaloyan20-May-04 21:52
kaloyan20-May-04 21:52 
GeneralRe: Problems with Screen.PrimaryScreen.Bounds Pin
Heath Stewart21-May-04 3:30
protectorHeath Stewart21-May-04 3:30 
GeneralSingle Instance of a class as long as it is in use Pin
SmithaC20-May-04 3:03
SmithaC20-May-04 3:03 
GeneralRe: Single Instance of a class as long as it is in use Pin
Grimolfr20-May-04 3:08
Grimolfr20-May-04 3:08 
GeneralRe: Single Instance of a class as long as it is in use Pin
Heath Stewart20-May-04 3:24
protectorHeath Stewart20-May-04 3:24 
You could use either .NET Remoting or a ContextBoundObject to do this as well. You can register a well-known object as a singleton in .NET Remoting with a lifetime of X number of some time unit. When no other objects reference that object, the .NET Remoting infrastructure disposes the remoting object.

With a ContextBoundObject and a custom proxy (using the ProxyAttribute - see the .NET Framework SDK documentation for more information and an example) so that you do the same thing without worrying about registering well-known objects.

Your custom proxy could, for example, catch the IConstructionCallMessage. In that case, you either create an instance of your object or return an existing instance. This object can still take advantage of the .NET Remoting infrastructure, such as the lifetime management services. See the ContextBoundObject.InitializeLifetimeService method documentation (inheritted from and defined by MarshalByRefObject) in the .NET Framework SDK for more information.

Using a ContextBoundObject would probably be the easiest way to acheive what you want. This way, your client code can still new the class without knowing whether it receives a new or existing instance, and you can use AOP (Aspect-Oriented Programming - see Aspect-Oriented Programming / Aspect-Oriented Software Design[^] by Marc Clifton on this site).

You could use mutexes to solve this problem with a singleton pattern, but then your calling code is required to increment and decrement the reference count (like with COM, though ATL helps aleviate that). With an AOP approach, you don't need to worry about it. The .NET Remoting infrastructure takes care of this for you and you can always override certain methods and implement the right interfaces (like ILease and ISponsor) to change this behavior if you want (or return null from InitializeLifetimeService to keep the object alive indefinitely). It's definitely worth checking out.

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: Single Instance of a class as long as it is in use Pin
scadaguy20-May-04 5:57
scadaguy20-May-04 5:57 
GeneralA general question about objects Pin
Christer Claesson20-May-04 2:47
Christer Claesson20-May-04 2:47 
GeneralRe: A general question about objects Pin
Heath Stewart20-May-04 3:08
protectorHeath Stewart20-May-04 3:08 
GeneralRe: A general question about objects Pin
Christer Claesson20-May-04 3:30
Christer Claesson20-May-04 3:30 
GeneralRe: A general question about objects Pin
Heath Stewart20-May-04 3:41
protectorHeath Stewart20-May-04 3:41 
GeneralRe: A general question about objects Pin
Christer Claesson20-May-04 4:02
Christer Claesson20-May-04 4:02 
GeneralRe: A general question about objects Pin
Grimolfr20-May-04 3:13
Grimolfr20-May-04 3:13 
GeneralRe: A general question about objects Pin
Heath Stewart20-May-04 3:34
protectorHeath Stewart20-May-04 3:34 
GeneralRe: A general question about objects Pin
Grimolfr20-May-04 5:09
Grimolfr20-May-04 5:09 
GeneralRe: A general question about objects Pin
Heath Stewart20-May-04 5:25
protectorHeath Stewart20-May-04 5:25 
GeneralRe: A general question about objects Pin
Grimolfr20-May-04 5:52
Grimolfr20-May-04 5:52 
GeneralRe: A general question about objects Pin
Heath Stewart20-May-04 5:57
protectorHeath Stewart20-May-04 5:57 
GeneralOne more simple question if someone have time. Pin
Christer Claesson20-May-04 5:18
Christer Claesson20-May-04 5:18 
Generaldll that is seen by multiple exe's Pin
jspano20-May-04 2:21
jspano20-May-04 2:21 
GeneralRe: dll that is seen by multiple exe's Pin
Heath Stewart20-May-04 3:01
protectorHeath Stewart20-May-04 3:01 
GeneralRe: dll that is seen by multiple exe's Pin
jspano20-May-04 14:31
jspano20-May-04 14:31 
GeneralXmlSerializer Pin
Sean Rock20-May-04 1:45
Sean Rock20-May-04 1:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.