通过CMD线消息传递单一实例的应用程序? [英] Single instance app with message passing via cmd line?

查看:114
本文介绍了通过CMD线消息传递单一实例的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#应用程序,通过命令行启动。通常,数据是通过命令行传递,如加(程序 - 一个字符串)。我想应用程序被打开只有一个实例,如果多个字符串是通过命令行加我想单实例来了解它,并更新自己。我可以把数据导入到数据库中正确和味精正在运行的实例或味精和数据传递给正在运行的实例,让它把它的数据库和更新。

I have a C# app that is launched via command line. Usually data is pass through the command line such as add (app -a string). I would like only ONE instance of the app to be opened and if more strings are added via command line i would like the single instance to know about it and update itself. I can either put the data into the database properly and msg the running instance or msg and pass the data to the running instance and let it put it in the db and update itself.

我将如何做到这一点在C#.NET? (3.5)

How would i do this in C# .NET? (3.5)

推荐答案

应用程序应该创建一个命名管道的第一个实例,应用程序的后续实例将无法创建相同的命名管道,而应尝试打开指定的管使用。一旦打开,该字符串(或任何真正的数据)可以被转移到应用程序的已运行的实例。命名管道可以被关闭,应用程序可能会退出。

The first instance of the application should create a named pipe, subsequent instances of the application would fail to create the same named pipe and should instead attempt to open the named pipe for use. Once opened, the string (or really any data) can be transferred to the already running instance of the app. The named pipe can then be closed and the app could exit.

另外,你可以使用.NET远程处理,并注册一个众所周知的类型,应用程序的其他实例可以激活,类似于上面所描述的行为。

Alternatively, you could use .NET Remoting and register a well-known type that other instances of the application could activate, with behavior similar to what is described above.

最后,在IPC或进程间通信快速搜索可以开拓其他的替代品。但我相信,命名管道的方法是最清洁,将是最容易实现的/扩展。

Ultimately, a quick search on "IPC" or "Inter-Process Communication" may open up other alternatives. But I believe the named pipe approach is the cleanest and would be the easiest to implement/extend.

有关首次运行检测,你也可以创建一个命名的互斥体,如果你选择不表现出对多运行故障状态(例如一个IPC机制,采用了数据库作为中间人对于数据共享)。没有两个处理可以创建具有相同名称的互斥体,这是相同的约束与命名管道,因为这些名称是由内核管理(因此跨越的过程中,会话,桌面和窗口站的界限。)

For "first-run" detection you can also create a named mutex, in case you opt for an IPC mechanism that doesn't exhibit a failure condition on multiple-runs (for example, using a database as a middle-man for data share). No two processed can "Create" a mutex with the same name, this is the same constraint as with Named pipes, as these names are managed by the Kernel (and thus span process, session, desktop and window station boundaries.)

这篇关于通过CMD线消息传递单一实例的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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