可以在一个Singleton类一个DLL内跨进程共享? [英] Can a Singleton Class inside a DLL be shared across processes?

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

问题描述

我创建一个定制达网络硬件框架,将被其他程序员可以用于控制某些硬件。他们将一个引用添加到我们的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).

该Singleton模式似乎是我所需要的,但它仅适用于你的进程内的多个线程。我可能是完全错误的,但这里是C#code我公司目前拥有的一个例子。我不禁感到,设计是不正确。我希望我能分享更多的具体信息,但我不能。

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);
	}
}

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

The first launched application gives an output of:

preSS进入夺取资源!

Press enter to capture the resource!

1:2009/6/24上午08时27分34秒。

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

3:2009/6/24上午08时27分34秒。

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

preSS进入关闭的进程!

Press enter to close the process!

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

The second application gives an output of:

preSS进入夺取资源!

Press enter to capture the resource!

9:2009/6/24上午08时27分35秒。

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

10:2009/6/24上午八时27分35秒。

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

preSS进入关闭的进程!

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.

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

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