访问公共变量 [英] Accessing common variables

查看:124
本文介绍了访问公共变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

3个应用程序同时运行.
是否有一种简单的方法可以从任何一个变量中访问相同的变量?
首先想到的是使用Windows注册表或文件.

有什么想法吗?

3 applications running at the same time.
Is there an easy way of accessing the same variables from any of them?
First thought is to use the Windows Registry or File.

Any thoughts?

推荐答案

坏主意!

如果您确实想这样做,则是专门为这种情况设计的一个IPC原语:Shared Memory.但是,该原语在.NET中不可用-我认为是有充分理由的.

下面显示了一种基于C ++/CLI的解决方案:
http://stackoverflow.com/questions/439787/how-to-implement-共享内存网络 [ ^ ].

我建议您使用VB.NET和P/Invoke所需的Windows API代替:CreateFileMappingMapViewOfFileCopyMemoryCloseHandle.参见:
http://msdn.microsoft.com/en-us/library/aa366551 (v = vs.85).aspx [ http://msdn.microsoft.com/en-us/library/system.threading .mutex.aspx [ ^ ].

共享内存和互斥锁的一个问题是它们将需要系统唯一的名称,而您需要设计这些名称并保证其唯一性.

—SA
Bad idea!

If you really want to do it, the is one IPC primitive designed specially for this case: Shared Memory. But this primitive is not available in .NET — I think, for a good reason.

One solution based on C++/CLI is shown here:
http://stackoverflow.com/questions/439787/how-to-implement-shared-memory-in-net[^].

I would suggest you use VB.NET and P/Invoke required Windows API instead: CreateFileMapping, MapViewOfFile, CopyMemory and CloseHandle. See:
http://msdn.microsoft.com/en-us/library/aa366551(v=vs.85).aspx[^].

Don''t forget to interlock access to shared memory using the named Mutex (only named mutex can be used for IPC). See http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx[^].

One problem of both shared memory and mutex is that they will need system-unique names you will need to devise and guarantee uniqueness.

—SA


您可以使用SaveSetting/GetSetting方法写入值并从注册表中检索值.
you could use the SaveSetting / GetSetting method to write the values and retrieve the values from the registry.
SaveSetting("appname", "section", "key", "value")
GetSetting("appname", "section", "key")

可能不是最优雅的解决方案,但它可行且简单.

Probably not the most elegant solution but it works and is easy.


这篇关于访问公共变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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