在同一应用程序的多个实例之间传递数据
Passing data between multiple instances of same application
我有一个 WPF 客户端应用程序,它在任何时候最多可以有一个实例,如果用户 运行 是第二个实例,它应该将输入参数传递给应用程序的第一个实例并退出。
即我 运行 第一个参数 "hello" 的实例,然后再次使用新参数 "welcome" 启动同一应用程序的新实例,不,第二个实例应该将数据 "welcome" 传递给第一个实例并退出。
除了 WCF 和 .Net 远程处理之外,还有什么最简单干净的方法吗?
目前我可以使用 WindowsEventHandle 使第一个实例处于活动状态,但无法传递数据。这是在同一个登录中的同一台机器上
非常基本,但您可以简单地写入一个文件,让另一个应用程序实例监视该文件,以根据当前时间戳(精确到毫秒)为具有唯一消息 ID 的新命令监视文件。
当然可以。我们尝试了多种方法,但使用互斥锁的方法似乎效果最好。
这里有一些示例:
How can I check for a running process per user session?
一篇关于互斥体的好文章http://odetocode.com/blogs/scott/archive/2004/08/20/the-misunderstood-mutex.aspx
一个简单的实现
如果您进行更深入的搜索,我相信 SO 中有一个很好的示例,它展示了如何使用互斥事件来触发另一个应用程序实例中的某些内容
看看 Memory-Mapped Files 从 .NET 4 开始,它们就可用了,甚至可以与非托管应用程序共享内存。
Non-persisted memory-mapped files Non-persisted files are
memory-mapped files that are not associated with a file on a disk.
When the last process has finished working with the file, the data is
lost and the file is reclaimed by garbage collection. These files are
suitable for creating shared memory for inter-process communications
(IPC).
我有一个 WPF 客户端应用程序,它在任何时候最多可以有一个实例,如果用户 运行 是第二个实例,它应该将输入参数传递给应用程序的第一个实例并退出。 即我 运行 第一个参数 "hello" 的实例,然后再次使用新参数 "welcome" 启动同一应用程序的新实例,不,第二个实例应该将数据 "welcome" 传递给第一个实例并退出。
除了 WCF 和 .Net 远程处理之外,还有什么最简单干净的方法吗?
目前我可以使用 WindowsEventHandle 使第一个实例处于活动状态,但无法传递数据。这是在同一个登录中的同一台机器上
非常基本,但您可以简单地写入一个文件,让另一个应用程序实例监视该文件,以根据当前时间戳(精确到毫秒)为具有唯一消息 ID 的新命令监视文件。
当然可以。我们尝试了多种方法,但使用互斥锁的方法似乎效果最好。
这里有一些示例: How can I check for a running process per user session?
一篇关于互斥体的好文章http://odetocode.com/blogs/scott/archive/2004/08/20/the-misunderstood-mutex.aspx
一个简单的实现
如果您进行更深入的搜索,我相信 SO 中有一个很好的示例,它展示了如何使用互斥事件来触发另一个应用程序实例中的某些内容
看看 Memory-Mapped Files 从 .NET 4 开始,它们就可用了,甚至可以与非托管应用程序共享内存。
Non-persisted memory-mapped files Non-persisted files are memory-mapped files that are not associated with a file on a disk. When the last process has finished working with the file, the data is lost and the file is reclaimed by garbage collection. These files are suitable for creating shared memory for inter-process communications (IPC).