检查桌面应用程序是否已经在运行 [英] Check if the desktop application is already running

查看:164
本文介绍了检查桌面应用程序是否已经在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究基于Java的桌面应用程序。一个要求是,如果应用程序正在运行,并且用户尝试再次启动该应用程序,它应该执行诸如显示警报等操作。

I am working on a java based desktop application. One requirement is that if application is running and user try to start the application again it should do something like show some alert etc.

我在用户运行时为此使用了文件在应用程序中,它将一些文本添加到文件中,然后将其保存在本地系统中的某个位置,当用户再次尝试运行该应用程序时,我会检查文件并相应地处理这种情况,但是这样做存在一些问题,例如,如果应用程序异常终止我无法删除该文件,在这种情况下,当用户尝试运行该应用程序时,系统会显示警报,表明该应用程序已在运行,但实际上该应用程序异常终止。

I used file for this, when user run the app it add some text into a file and save it somewhere on the local system and when user try to run the app again i check the file and handle the situation accordingly, but there are some issue with this, like if application is abnormally terminated i am not able to remove that file and in that case when user tries to run the app system shows alert that application is already running but actually app terminated abnormally.

一些研发,发现我可以阅读Windows任务管理器列表,然后可以获取并检查我的应用程序是否正在运行。它工作正常,但在测试过程中出现问题,即Windows中有一个名为 tasklist的软件/实用工具,转到开始->运行,然后键入tasklist,它将显示任务列表。如果此实用程序未安装在您的系统上或由于某种原因被删除/损坏,那么它将无法正常工作,并且会出现相同的问题,即用户可以多次启动该应用程序。这是访问任务管理器项的代码

I did some R&D and found that i can read windows task manager list and then i can get and check my app if it is running. it was working fine but during testing i got an issue i-e there is a software/utility in windows named "tasklist" go to "start --> run" and type tasklist it will show you the task list. if this utility is not installed on your system or removed/corrupted for some reason then this will not work and same issue occur that user can start the application more then one time. Here is code to access the taskmanager items

Process p = Runtime.getRuntime().exec(
                System.getenv("windir") + "/system32/" + "tasklist.exe");

BufferedReader input = new BufferedReader(new InputStreamReader(
                p.getInputStream()));
while ((line = input.readLine()) != null) {
/*
Make the comparison here and show alert.
*/

}

问题:什么是检查应用程序是否已在运行的好方法?我将Java用于应用程序,并使用高级安装程序来创建应用程序的安装程序。

Question: What is a good way to check if the app is already running? I am using Java for application, and advanced installer to create the installer of the application.

推荐答案

启动应用程序。与 Java Web Start 并使用 SingleInstanceService 。请参见演示。 (演示和示例代码)的实例。

Launch the app. with Java Web Start and use the SingleInstanceService. See the demo. of the SingleInstanceService for (demo. and) example code.


am使用..高级安装程序来创建安装程序

am using .. advanced installer to create the installer

请注意,JWS:


  1. 也是应用程序安装程序。因此,我建议将此路径作为使用高级安装程序的替代方法。

  2. 由Java(Sun / Oracle)的制造商提供。

这篇关于检查桌面应用程序是否已经在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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