如何在 C# 中查询另一个正在运行的应用程序中的变量?

发布于 2024-08-11 09:43:37 字数 127 浏览 2 评论 0原文

我有一个应用程序,启动时会检查其自身的重复进程。

这部分我是对的 - 但我需要的是检查原始运行过程中的状态变量以便运行一些逻辑。

那么:如何使变量(例如 bool)对其他应用程序公开可用,以便它们可以查询它?

I have an app that, when launched, checks for duplicate processes of itself.

That part I have right - but what I need is to check a state variable in the original running process in order to run some logic.

So: how do I make a variable (e.g. bool) available publicly to other applications so they can query it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一袭水袖舞倾城 2024-08-18 09:43:37

有很多方法可以做到这一点。一种非常原始的方法是从文件中读/写。旧的 win32 方式是使用 PostMessage 。更多 .NET 方式是使用远程处理或 WCF 和 命名管道

.NET 4 还获得了对 内存映射文件

这是一篇相当详尽的文章,描述了几种不同的方法,包括对 .NET 4 之外的内存映射文件的支持
http://www.codeproject.com/KB/threads/csthreadmsg.aspx

There are a bunch of ways to do this. A very primative way would be to read/write from a file. The old win32 way would be to use PostMessage. The more .NET way would be to use remoting or WCF and Named Pipes.

.NET 4 is also getting support for Memory Mapped files.

Here is a pretty thorough looking artcile describing a few different approaches including support for Memory Mapped files outside of .NET 4
http://www.codeproject.com/KB/threads/csthreadmsg.aspx

二智少女 2024-08-18 09:43:37

最简单的:创建一个文件,然后在其中写入一些内容。

更高级,并且正确完成后更强大的是使用 WCF,您使用命名管道仅在本地计算机上设置某些通信通道。

The easiest: Create a file, and write something in it.

More advanced, and when done correctly more robust, is using WCF, you use named pipes to setup some communication channel on the local computer only.

停顿的约定 2024-08-18 09:43:37

如果您使用 Mutex 来检查另一个进程是否正在运行(你应该是),你可以使用另一个互斥体,其锁定状态将是你正在寻找的布尔标志。

If you're using a Mutex to check whether another process is running (you should be) you could use another Mutex whose locked state would be the boolean flag you're looking.

阿楠 2024-08-18 09:43:37

执行此操作的标准方法是使用 Windows API 创建并锁定互斥体。第一个打开的应用程序将创建并锁定互斥锁。该应用程序的任何后续执行都将无法获取它,然后可能会关闭。

The standard way of doing this is to use the Windows API to create and lock a mutex. The first app to open will create and lock the mutex. Any subsequent executions of the app will not be able to get it and can then shutdown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文