DLL 中的单例类可以跨进程共享吗? [英] Can a Singleton Class inside a DLL be shared across processes?

查看:31
本文介绍了DLL 中的单例类可以跨进程共享吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个自定义的 .net 硬件框架,其他程序员将使用它来控制某些硬件.他们将添加对我们的 DLL 的引用以访问我们的硬件框架.我需要一个可以从多个应用程序(进程)访问的共享类.

I am creating a custom .net hardware framework that will be used by other programmers to control some hardware. They will add a reference to our DLL to get to our hardware framework. I am in need of a shared class that will be accessed from multiple applications (processes).

单例模式似乎是我所需要的,但它仅适用于您进程中的多个线程.我可能完全错了,但这是我目前拥有的 C# 代码示例.我不禁觉得设计不正确.我希望我可以分享更具体的信息,但我不能.

The singleton pattern seems to be what I need but it only works for multiple threads inside your process. I could be completely wrong but here is an example of the C# code I currently have. I can't help to feel that the design is incorrect. I wish I could share more specific information but I can't.

  • 我必须强调,我无法控制客户应用程序.解决方案必须包含在框架 (DLL) 本身中.

框架:(共享 DLL)

The Framework: (Shared DLL)

public class Resources
{
    static readonly Resources m_instance = new Resources();

    public string Data;

    private Resources()
    {
        Data = DateTime.Now.ToString();
    }

    public static Resources Instance
    {
        get
        {
            return m_instance;
        }
    }
} 

测试应用:(最终客户应用)

The Test Application: (eventually customer app)

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Press enter to capture the resource!");
        Console.ReadLine();

        var resources = Resources.Instance;
        Console.WriteLine("
{0}: {1}
", Thread.CurrentThread.ManagedThreadId, resources.Data);

        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += WorkerDoWork;
        worker.RunWorkerAsync();

        while (worker.IsBusy)
        {
            Thread.Sleep(100);
        }

        Console.WriteLine("Press enter to close the process!");
        Console.ReadLine();
    }

    static void WorkerDoWork(object sender, DoWorkEventArgs e)
    {
        var resources = Resources.Instance;
        Console.WriteLine("
{0}: {1}
", Thread.CurrentThread.ManagedThreadId, resources.Data);
    }
}

第一个启动的应用程序输出:

The first launched application gives an output of:

按回车捕获资源!

1:2009 年 6 月 24 日上午 8:27:34

1: 6/24/2009 8:27:34 AM

3:2009 年 6 月 24 日上午 8:27:34

3: 6/24/2009 8:27:34 AM

按回车关闭进程!

第二个应用程序给出的输出:

The second application gives an output of:

按回车捕获资源!

9:2009 年 6 月 24 日上午 8:27:35

9: 6/24/2009 8:27:35 AM

10:2009 年 6 月 24 日上午 8:27:35

10: 6/24/2009 8:27:35 AM

按回车关闭进程!

结论:

我希望两个应用程序都返回该类第一次实例化时间的相同字符串.

I would like to see both applications return the same string of the time of the first instantiation of the class.

如您所见,单例适用于进程内的多线程,但不适用于跨进程.也许这无法完成,因为我似乎找不到任何解决方案.

As you can see the singleton works for the multiple thread inside the process but not cross processes. Maybe this can't be done for I can't seem to find any solution.

推荐答案

您不能使用单例在应用程序之间进行同步.每个都在自己的应用程序空间中运行,并且出于安全考虑无法访问内存/对象/等.另一个没有通信方法(如远程)要同步两者,他们必须远程进入第三个程序.

You cannot use a singleton to sync across applications. Each runs in its own application space, and as a matter of security cannot access memory/objects/etc. from the other without a method of communication (like remoting) To sync the two they would have to remote into a third program.

这篇关于DLL 中的单例类可以跨进程共享吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆