我正在编写一个小程序,该程序根据按下的按钮运行三个代码示例之一。使用第三个按钮,代码示例应该运行直到 JFrame 关闭,或者直到按下按钮(我真的不介意它是如何停止的,只要用户有停止循环的方法)。代码运行,并添加了8秒的延迟,以确保代码在循环之前完成运行。
我该如何实现这个?该程序在循环时似乎不会终止,即使我尝试通过单击 JFrame 中的关闭按钮来关闭它也是如此。
程序的主要部分如下所示:
public class WaspmoteSim extends JFrame implements ActionListener {
public WaspmoteSim() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
//getContentPane().setLayout(new GridLayout(1, 3, 10, 10));
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(30,30,30,30);
c.ipadx = 10;
c.ipady = 30;
setSize(700, 150);
setLocation(100, 100);
JButton button1 = new JButton("Demonstration Mode");
button1.addActionListener(this);
add(button1, c);
JButton button2 = new JButton("Distribution Fitting Mode");
button2.addActionListener(this);
add(button2, c);
JButton button3 = new JButton("Operational Mode");
button3.addActionListener(this);
add(button3, c);
setVisible(true);
}
public static void main(String[] args) {
new WaspmoteSim();
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("Demonstration Mode")) {
try {
DemoMethod();
} catch (IOException ex) {
Logger.getLogger(WaspmoteSim.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (command.equals("Distribution Fitting Mode")) {
try {
FittingMethod();
} catch (IOException ex) {
Logger.getLogger(WaspmoteSim.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (command.equals("Operational Mode")) {
try {
OperationsMethod();
} catch (IOException ex) {
Logger.getLogger(WaspmoteSim.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(WaspmoteSim.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我想要在循环上运行的代码如下所示:
public void OperationsMethod() throws IOException, InterruptedException {
while(true) {
String workingDir = System.getProperty("user.dir");
System.out.println(workingDir);
Process proc;
proc = Runtime.getRuntime().exec("cmd.exe /C C:\\Progra~1\\R\\R-3.2.1.\\bin\\Rscript.exe " + workingDir + "\\Fitter.r");
TimeUnit.SECONDS.sleep(8);
}
}
最佳答案
您可以遵循的程序如下:
1) 使用 ProcessBuilder 创建一个进程并将其存储以供以后使用(您可以为此使用持有者单例类“ProcessHolder”)。
2) 向您的按钮注册一个事件函数,该函数将您的按钮注册一个事件函数,该按钮可以访问 ProcessHolder
,并使用 destroy()
终止该进程> 方法。
查看更多here .
//Process Holder singleton
public class ProcessHolder {
private Map<String, Process> _processes;
private static ProcessHolder _processHolder;
private ProcessHolder() {
_processes = new HashMap<>();
}
public static ProcessHolder getInstance() {
if(_processHolder == null) {
_processHolder = new ProcessHolder();
}
return _processHolder;
}
public void getProcesses() {
return _processes;
}
}
//......
public class WaspmoteSim extends JFrame implements ActionListener {
public static final String YOUR_PROCESS_NAME = "Rscript.exe";
public WaspmoteSim() {
//....
JButton button = new JButton("Destroy Process");
Process process = Runtime.getRuntime().exec(
System.getenv("cmd.exe /C C:\\Progra~1\\R\\R-3.2.1.\\bin\\" + YOUR_PROCESS_NAME + " + workingDir + "\\Fitter.r");
ProcessHolder.getInstance().getProcesses().put(processName, p);
button.addActionListener(this);
//....
}
//Callback:
@Override
public void actionPerformed(ActionEvent e) {
ProcessHolder.getInstance().getProcesses().get(YOUR_PROCESS_NAME).destroy();
}
}
还有一个通知:
你正在做:
while(true) {
Process proc;
proc = Runtime.getRuntime().exec("cmd.exe /C C:\\Progra~1\\R\\R-3.2.1.\\bin\\Rscript.exe " + workingDir + "\\Fitter.r");
TimeUnit.SECONDS.sleep(8);
}
这将尝试每 8 秒启动一个新进程 (!)。为了启动一个进程,只需执行一次,存储它,然后在需要时使用 destroy 杀死它,除非您的目标确实是创建这样的新进程。
关于java - 如何在 Java 中运行进程直到按下按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33063203/