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

查看:140
本文介绍了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)

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("\r\n{0}: {1}\r\n", 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("\r\n{0}: {1}\r\n", Thread.CurrentThread.ManagedThreadId, resources.Data);
    }
}

第一个启动的应用程序提供以下输出: p>

The first launched application gives an output of:


按Enter键捕获资源!

Press enter to capture the resource!

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

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

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

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

按Enter关闭流程!

Press enter to close the process!

第二个应用程序输出:


按回车来捕获资源!

Press enter to capture the resource!

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

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

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

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

按enter键关闭流程!

Press enter to close the process!

结论

我想看到两个应用程序返回相同字符串的类的第一个实例化的时间。

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天全站免登陆