C#中的单例实现问题

发布于 2024-10-31 18:09:10 字数 2285 浏览 1 评论 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();
        }
    }
}

我正在尝试进行 simgleton 实现,但我的方法有问题。它始终从每个控制台应用程序创建 _instanceA 和 _instanceB 的新实例。

有人可以指出我需要在这里做什么吗?

--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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

—━☆沉默づ 2024-11-07 18:09:10

您需要命名互斥体来实现进程间同步。

You would need named Mutexes for inter-process synchronization.

五里雾 2024-11-07 18:09:10

在两个应用程序之间共享对象实例有点困难,因为默认情况下它们在单独的应用程序域中运行。为了完成我认为您想要做的事情,您需要

  • 跨应用程序域边界进行编组,或者
  • 在共享应用程序域中运行这两个进程。编写第三个进程(一个 shell),负责在共享应用程序域中生成/托管其他两个进程。

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

在 AppDomain 之间共享数据

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文