开发者

singleton implementation problem in C#

--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("开发者_如何学JAVAp1","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?


You would need named Mutexes for inter-process synchronization.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜