控制程序的实例数(跨平台) [英] Controlling number of instances of a program (Cross platform)

查看:63
本文介绍了控制程序的实例数(跨平台)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有没有办法确保只有一个应用程序实例在物理机器上运行
。我希望以跨平台的方式做到这一点。

我有一个想法是获取正在运行的进程列表并检查列表中的b $ b $ - 然后(推测) ),检查代码将永远在列表中的
上 - 所以这有点失败了。


有什么想法吗?


非常感谢

Hi,

Is there a way to ensure that only one instance of an application runs
on a physical machine. I would want to do this in a cross platform way.
One ide I have is to obtain a list of the running processes and checking
the list - but then (presumably), the checking code would always be on
the list - so this defeats the point somewhat.

Any ideas?

many thanks

推荐答案

Paul Tremblay写道:
Paul Tremblay wrote:

<有没有办法确保只有一个应用程序实例在物理机器上运行
Hi,

Is there a way to ensure that only one instance of an application runs
on a physical machine.




没有便携方式。在UNIX上它甚至都不容易。枚举

Windows上的进程工作(我相信NT衍生的唯一方式

内核)。



No portable way. It''s not even overly easy on UNIX. Enumerating
the processes on Windows works (and I believe the only way on NT-derived
kernels).


Paul Tremblay写道:
Paul Tremblay wrote:


有没有办法确保只有一个应用程序实例在物理机器上运行
。我想以跨平台的方式做到这一点。
我有一个想法是获取正在运行的进程列表并检查列表 - 但是然后(大概),检查代码将永远是在列表上 - 所以这在某种程度上打败了这一点。

任何想法?
Hi,

Is there a way to ensure that only one instance of an application runs
on a physical machine. I would want to do this in a cross platform way.
One ide I have is to obtain a list of the running processes and checking
the list - but then (presumably), the checking code would always be on
the list - so this defeats the point somewhat.

Any ideas?




没有真正的平台无限制的方式。一种常用的方法是

进程只是在开始时创建一个文件并在结束时删除它。

在启动之前,它会检查文件是否已经存在以及是否存在它会立即退出。

关于这一点的主要问题是以独立的方式处理程序崩溃并离开的

案例那里的文件。



There is no really platform indepedant way. One often used way is that the
process simply creates a file at start and removes it at the end.
Before starting up, it checks whether the file already exists and if it
does, exits immediately.
The main problem about this is handling in a platform independant way the
case that the program crashed and left the file there.


>有没有办法确保只有一个应用程序实例在物理机器上运行
> Is there a way to ensure that only one instance of an application runs
。我想以跨平台的方式做到这一点。
我有一个想法是获取正在运行的进程列表并检查列表
on a physical machine. I would want to do this in a cross platform way.
One ide I have is to obtain a list of the running processes and checking
the list




获取此列表是特定于平台的。所以你必须为你想要运行的每个平台实现这个
。也许

有一个库已经这样做了。


你可以让你的程序尝试绑定到特定端口的套接字。

如果另一个实例试图绑定到同一个端口,那么它将失败。

然后您将通过退出处理此故障,只留下第一个

实例运行。


或者当实例启动时,它可以检查存储在

文件中的标志,该标志指示另一个实例是否已在运行。



Put obtaining this list is platform specific. So you''d have to
implement this for every platform which you want to run on. Maybe
there''s a library which already does this.

You could have your program try to bind to a socket to specific port.
If another instance tries to bind to the same port, then it will fail.
You would then handle this failure by exiting, leaving only the first
instance running.

Or when an instance starts, it could check a flag that''s stored in a
file, which indicates if another instance is already running.


这篇关于控制程序的实例数(跨平台)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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