0

--ConsoleApplication 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    public class MsgService
    {
        private static CreateConnectionToA _instanceA;
        private static CreateConnectionToB _instanceB;

        protected MsgService()
        {

        }

        public static MsgService GetInstanceA(string paramA, string paramB)
        {

            if (_instanceA != null)
            {
                return _instanceA;
            }

            return _instanceA = new CreateConnectionToA("p1","p2");
        }


        public static MsgService GetInstanceB(string paramA, string paramB)
        {

            if (_instanceB != null)
            {
                return _instanceB;
            }

            return _instanceB = new CreateConnectionToB("p1", "p2");
        }


    }




}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class CreateConnectionToB : MsgService
    {
        public CreateConnectionToB(string param1, string Param2)
        {

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class CreateConnectionToA : MsgService
    {
        public CreateConnectionToA(string param1, string Param2)
        {

        }

    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

            MsgService.GetInstanceA("p1", "p2");

            Console.Read();
        }
    }
}

--ConsoleApplication 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press");
            Console.Read();

            ConsoleApplication2.MsgService.GetInstanceA("p1", "p2");
            Console.Read();
        }
    }
}

I am trying to Make simgleton implementation but something is wrong with my approach. It always creates new instance of _instanceA and _instanceB from each console application.

Can someone please point me out what needs to be done here?

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
Asdfg
  • 11,362
  • 24
  • 98
  • 175

2 Answers2

3

You would need named Mutexes for inter-process synchronization.

GregC
  • 7,737
  • 2
  • 53
  • 67
  • 1
    straight up, yep. if we are not contrained by implementation choices, and simply attempting to achieve a remote service that behaves as a singleton, i would strongly suggest looking at WCF (which has capability to do so). – johnny g Apr 11 '11 at 16:13
  • http://www.yoda.arachsys.com/csharp/singleton.html Jon Skeet's singleton is written on a premise that it lives within one process. Good read nonetheless. – GregC Apr 11 '11 at 16:21
  • example for the Named Mutex please? – Asdfg Apr 11 '11 at 16:24
  • @Asdfg: could you describe exactly what you're trying to do, without using the word singleton and word service? – GregC Apr 11 '11 at 16:24
  • I am trying to create one instance of _instanceA and _instanceB regardless of calls. From ConsoleApplication1, it works fine but from ConsoleApplication2, null check fails and it creates new instance. I want _instanceA and _instanceB to be in the memory. – Asdfg Apr 11 '11 at 16:30
0

Sharing an object instance between two applications is kinda hard, since they run in separate appdomains, by default. To accomplish what I think you're trying to do, you'll need to either

  • marshal across appdomain boundaries with, or
  • run the two processes in a shared appdomain. Write a 3rd process — a shell — that's responsible for spawning/hosting the other two processes in a shared appdomain.

http://www.codeproject.com/KB/dotnet/AppDomainMemImprovement.aspx

Sharing data between AppDomains

Community
  • 1
  • 1
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135