Check if the desktop application is already running

a 夏天 提交于 2019-12-12 16:26:46

问题


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.

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.
*/

}

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.


回答1:


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

am using .. advanced installer to create the installer

Note that JWS:

  1. Is also an application installer. So I am suggesting this route as an alternative to using 'advanced installer'.
  2. Is provided by the makers of Java (Sun/Oracle).



回答2:


Check out http://ss64.com/nt/wmic.html and try

String line;
try {
    Process proc = Runtime.getRuntime().exec("wmic.exe");
    BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    OutputStreamWriter oStream = new OutputStreamWriter(proc.getOutputStream());
    oStream .write("process where name='THE NAME OF YOUR PROCESS.exe'");
    oStream .flush();
    oStream .close();
    while ((line = input.readLine()) != null) {
        System.out.println(line);
    }: 
    input.close();
} catch (IOException ioe) {
    ioe.printStackTrace();
}

Courtesy:How to detect via Java whether a particular process is running under Windows?



来源:https://stackoverflow.com/questions/17363820/check-if-the-desktop-application-is-already-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!