none
Class visible for multiple process RRS feed

  • Question

  • Hi I'm really confused about how can I solve this. 

    I need that one class that is already running in one process are available for new process. I found that use a singleton pattern is what I need (I guess), it works fine if I try to call all under the same process, but if I use different process it doesn't works. 

    look this an example what I need.   First I create a class that is a singleton class, it means it allow only one instance.

     
      // "Singleton"   
      class Singleton  
      {  
        private static Singleton instance;  
     
        // Note: Constructor is 'protected'   
        protected Singleton()   
        {  
        }  
     
        public static Singleton Instance()  
        {  
          if (instance == null)  
            instance = new Singleton();  
     
          return instance;  
        }  
      } 

    Then I have two applications that call the singleton instance

      class App1  
      {  
          
        static void Main()  
        {  
          // Constructor is protected -- cannot use new   
          Singleton s1 = Singleton.Instance();  
     
          Console.Read();  
        }  
      }  
     
      //***The second application  
      class App2  
      {  
          
        static void Main()  
        {  
          // Constructor is protected -- cannot use new   
          Singleton s1 = Singleton.Instance();  
          
          Console.Read();  
        }  
      } 
    What I wait is that both use the same instance, I mean because the App1 call first to singleton when App2 call it use the same instance called by App1, but them call a separate instance, I guess because there are not under the same process, so what I need use to can call the same instance under multiple process?

    Thanks in advance.
    • Edited by Pinkyjong Monday, November 10, 2008 9:11 PM
    Monday, November 10, 2008 7:30 PM

All replies

  • Hi,

    It depends on .NET version you are using.
    In case of 2.0:
    You can use .NET Remoting to achieve this. .NET Remoting also has singleton concept, but it also has singlecall and client activated concepts which you can also consider as an option.
    In case of 3.0/3.5:
    Use Windows Communication Foundation plus IPC (inter-process communication). You can run service host in application App1 and connect to this service host from App2.

    Vitaliy Liptchinsky
    Monday, November 10, 2008 8:42 PM
  • I also try with .Net remoting, but I have the same problem in the example that I did when I get the object that I want to use If I call from the same process it return the same object but If I call from a second process it create a new instance, so it don't solve my problem, or maybe I'm doing something wrong.

    I follow this guide to do the example with remoting http://msdn.microsoft.com/en-us/library/ms998525.aspx 
    Tuesday, November 11, 2008 4:52 PM