如何在mono下强制执行应用程序的单个实例? [英] How to enforce single instance of an application under mono?

查看:118
本文介绍了如何在mono下强制执行应用程序的单个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我能够按如下所示在Windows上强制执行应用程序的单个实例.

So, I am able to enforce single instance of my application on Windows as follows.

[STAThread]
class method Program.Main(args: array of string);
begin
  var mutex := new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");
  if mutex.WaitOne(Timespan.Zero, true) then
  begin
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.ThreadException += OnThreadException;
    lMainForm := new MainForm;
    lMainForm.ShowInTaskbar := true;
    lMainForm.Visible := false;

    Application.Run(lMainForm);
  end
  else
    MessageBox.Show("Another copy running!!!");
end;

但是,在Mono上在Linux上运行相同的应用程序,此代码根本不起作用.我可以运行多个副本.我不知道是否与我像mono MyPro.exe这样在终端上启动应用程序有关.如果这是问题所在,在执行命令行之前是否需要传递一些值.

However, running the same application on Linux under mono this code does NOT work at all. I am able to run multiple copies. I don't know if it has to do with the fact that I am starting the application on the Terminal like mono MyPro.exe. If this is the problem, do you need to pass some values before you execute the command line.

预先感谢

推荐答案

正如Adrian Faciu提到的那样,您需要在mono中启用共享内存,以使您的方法有效,但这不是最佳方法(这是被它禁用的原因.即使我现在不记得确切的原因,它仍然是默认的.

You need to enable shared memory in mono as Adrian Faciu has mentioned to make your approach work, however this is not the best approach (there's a reason it's disabled by default in the first place, even if I can't remember now exactly why).

我过去使用过两种解决方案:

I've used two solutions in the past:

  • 基于文件的锁.创建一个已知文件,将pid写入该文件.在应用程序启动时,检查文件是否存在,以及是否存在该文件,请读取pid并检查是否有任何带有该pid的正在运行的进程(以便它可以从崩溃中恢复).并在退出时删除文件(首先创建它的实例).缺点是,如果几乎同时启动多个实例,则启动时会出现竞争条件.您可以通过文件锁定来改善这一点,但是您可能必须使用P/Invokes在Linux上进行正确的文件锁定(我不完全确定托管API会达到您的期望).

  • A file-based lock. Create a known file, write the pid into that file. At startup in your app check if the file exists, and if it exists read the pid and check if there are any running processes with that pid (so that it can recover from crashes). And delete the file upon exit (in the instance that created it in the first place). The drawback is that there is a race condition at startup if several instances are launched pretty much at the same time. You can improve this with file locking, but you may have to use P/Invokes to do the proper file locking on Linux (I'm not entirely sure the managed API would do what you'd expect).

基于袜子的锁.打开一个已知的端口.上面的优点是您不需要进行任何清理,也没有竞争条件.缺点是您需要一个固定的/已知的端口,并且其他一些程序可能碰巧同时使用该端口.

A socked-based lock. Open a known port. The advantage over the above is that you don't need to do any cleanup and there are no race conditions. The drawback is that you need a fixed/known port, and some other program might happen to use that exact port at the same time.

这篇关于如何在mono下强制执行应用程序的单个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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