java - 需要停止用户第二次单击 exe,而它已经在运行

标签 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 就会终止。第一次和第二次执行。

最佳答案

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

关于java - 需要停止用户第二次单击 exe,而它已经在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8225735/

相关文章:

java - android继承对象simplexml

python - 如何将base64字符串解码为其原始图像并在不保存的情况下打开它

C.exe "stopped working"错误

c++运行带参数的.exe文件

exe - 如何制作修改后的副本的EXE文件?

java - 一种封装java应用程序的方法

java - Java 是否有注释来指出 List 可能包含 null?

java - 如何处理ftl(freemarker)中CDATA标记内指定的html anchor 标记

java - Android/Java关闭onClick问题?

java - 如何通过 Android 应用程序查看公共(public) Google 日历