
单例模式似乎是我需要的,但它只适用于您的进程中的多个线程.我可能完全错了,但这里是我目前拥有的C#代码的例子.我不禁感到设计不正确.我希望我可以分享更多具体的信息,但我不能.
>我必须强调,我无法控制客户的申请.解决方案必须包含在框架(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; } }} 测试应用:(最终客户应用程序)
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",resources.Data); }} 第一个启动的应用程序提供以下输出:
Press enter to capture the resource!
1: 6/24/2009 8:27:34 AM
3: 6/24/2009 8:27:34 AM
Press enter to close the process!
第二个应用程序提供以下输出:
Press enter to capture the resource!
9: 6/24/2009 8:27:35 AM
10: 6/24/2009 8:27:35 AM
Press enter to close the process!
结论:
我想看到两个应用程序返回与该类的第一个实例化时间相同的字符串.
正如你可以看到,单程工作的过程中的多线程,但不是跨进程.也许这不能做,因为我似乎找不到任何解决方案.
解决方法 您不能使用单例来跨应用程序同步.每个运行在自己的应用程序空间中,并且作为安全性不能访问内存/对象/等等.从另一个没有通信方法(如遥控)要同步两个他们将不得不远程进入第三个程序. 总结以上是内存溢出为你收集整理的c# – DLL内的单例类可以跨进程共享吗?全部内容,希望文章能够帮你解决c# – DLL内的单例类可以跨进程共享吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)