需要阻止用户第二次单击exe,同时它已经在运行

时间:2011-11-22 11:02:48

标签: java exe

我开发了一个Java应用程序的exe,并从一个拇指驱动器运行它。执行需要一些时间。但我的最终用户认为它没有运行并且第二次点击。我需要阻止这一点。我需要在运行时停止连续点击exe。我使用shell脚本来检查exe是否正在运行。并显示exe已经运行的消息。并停止进一步的过程。我需要在exe运行第二次时发生这种情况。我无法弄清楚这一点。有什么方法可以在运行时禁用来自点击的exe。或者我如何使用检查它是否正在运行。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;


public class VBSUtils {

      private VBSUtils() {  }

      public static boolean isRunning(String process) {
        boolean found = false;
        try {
            File file = File.createTempFile("realhowto",".vbs");
            file.deleteOnExit();
            FileWriter fw = new java.io.FileWriter(file);

            String vbs = "Set WshShell = WScript.CreateObject(\"WScript.Shell\")\n"
                       + "Set locator = CreateObject(\"WbemScripting.SWbemLocator\")\n"
                       + "Set service = locator.ConnectServer()\n"
                       + "Set processes = service.ExecQuery _\n"
                       + " (\"select * from Win32_Process where name='" + process +"'\")\n"
                       + "For Each process in processes\n"
                       + "wscript.echo process.Name \n"
                       + "Next\n"
                       + "Set WSHShell = Nothing\n";

            fw.write(vbs);
            fw.close();
            Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
            BufferedReader input =
                new BufferedReader
                  (new InputStreamReader(p.getInputStream()));
            String line;
            line = input.readLine();
            if (line != null) {
                if (line.equals(process)) {
                  found = true;
                }
            }
            input.close();

        }
        catch(Exception e){
            e.printStackTrace();
        }
        return found;
      }



}

在我的主课上,我打电话给VBUtils。

 boolean result = VBSUtils.isRunning("myexe.exe");
if(result)
{
     msgBox("myexe is running. Please wait");
}
else
{
 // my part of execution.
}

如果我这样打电话,exe就会被终止。第一次和第二次执行。

3 个答案:

答案 0 :(得分:3)

最简单的方法是向用户呈现某种视觉反馈,因此他知道应用程序正在运行。 (例如,带有消息的控制台窗口,等待对话框......)

答案 1 :(得分:2)

您需要使用某种标记来指示您的应用正在运行。许多应用程序使用临时.pid文件。当您的应用程序启动时,它会检查.pid文件,如果有,则会退出并显示错误消息。如果没有.pid文件,那么它会创建一个并正常运行。

答案 2 :(得分:1)

首先,java提供了一个非常早期的启动画面:

http://download.oracle.com/javase/6/docs/api/java/awt/SplashScreen.html

此外,根据 RMI ,您可以在启动时连接到任何可能正在运行的实例并停止并传输命令行(“打开文件...”)。 RMI允许您播放远程服务器。