我正在创建一个将数据从日志文件发送到数据库的程序。程序应该在我按下运行按钮时启动,并在我按下停止按钮时停止。到目前为止,当我按下运行按钮时,我能够让它运行,但无法停止它。有人能帮我吗?
我要停止的程序是这样的(我不认为所有这些对于停止都很重要,但我会放整个程序):
private void RunActionPerformed(java.awt.event.ActionEvent evt){
// Creating a Timer object and scheduling the program to run every ten minutes
if(evt.getSource() == Run) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
public void run() {
try {
// THE CODE, WITH WHAT HAS TO BE DONE
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,ex);;
} catch (IOException ex) {
JOptionPane.showMessageDialog(null,ex);;
} catch (InstantiationException ex) {
JOptionPane.showMessageDialog(null,ex);;
} catch (IllegalAccessException ex) {
JOptionPane.showMessageDialog(null,ex);;
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null,ex);;
}
}
}, 0, 60 * 1000 * 10); //Number of minutes, until the program starts to run again
}
}
private void StopActionPerformed(java.awt.event.ActionEvent evt){
// TODO add your handling code here:
}
那么我应该怎么做才能阻止它呢?提前致谢