在我的应用程序中按下备份按钮时,创建数据库备份需要一些时间,因此我需要显示一个包含"请等待的jframe。"消息,并希望在备份过程完成时进行处理。以下是我的代码,但它从不显示jframe,但应用程序一直停留,直到进程结束。
try {
WaitView wait = new WaitView();
wait.setLocationRelativeTo(null);
wait.setVisible(true);
Process p = Runtime.getRuntime().exec("cmd /c mysqlbackup.bat", null, new File("C:\\MySQLBackups"));
p.waitFor();
wait.dispose();
MessageService.showMessageDialog(MessageService.BACKUP_SUCCESSFUL, "Successful", "info");
} catch (Exception ex) {
ex.printStackTrace();
}
答案 0 :(得分:0)
将代码包装在SwingUtilities.invokeLater()
中 Process p = Runtime.getRuntime().exec("cmd /c mysqlbackup.bat", null, new File("C:\\MySQLBackups"));
p.waitFor();
wait.dispose();
MessageService.showMessageDialog(MessageService.BACKUP_SUCCESSFUL, "Successful", "info");
但实际上最好在单独的线程中运行succh任务。检查SwingWorker示例。
答案 1 :(得分:0)
好吧,正如StanislavL上面指示的那样,我修改了下面的代码,And It Worked!
final WaitView wait = new WaitView();
wait.setLocationRelativeTo(null);
wait.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Process p = Runtime.getRuntime().exec("cmd /c mysqlbackup.bat", null, new File("C:\\MySQLBackups"));
p.waitFor();
wait.dispose();
MessageService.showMessageDialog(MessageService.BACKUP_SUCCESSFUL, "Successful", "info");
} catch (InterruptedException ex) {
Logger.getLogger(HomeView.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(HomeView.class.getName()).log(Level.SEVERE, null, ex);
}
}
});