我在使用JFrame时遇到问题,而在
连续运行代码.下面是我的代码:
>单击btnRun时,我调用了函数MainLoop():
ActionListener btnRun_Click = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MainLoop();
}
};
> MainLoop()的实现:
void MainLoop()
{
Hopper = new CHopper(this);
System.out.println(Hopper);
btnRun.setEnabled(false);
textBox1.setText("");
Hopper.getM_cmd().ComPort = helpers.Global.ComPort;
Hopper.getM_cmd().SSPAddress = helpers.Global.SSPAddress;
Hopper.getM_cmd().Timeout = 2000;
Hopper.getM_cmd().RetryLevel = 3;
System.out.println("In MainLoop: " + Hopper);
// First connect to the validator
if (ConnectToValidator(10, 3))
{
btnHalt.setEnabled(true);
Running = true;
textBox1.append("
Poll Loop
"
+ "*********************************
");
}
// This loop won't run until the validator is connected
while (Running)
{
// poll the validator
if (!Hopper.DoPoll(textBox1))
{
// If the poll fails, try to reconnect
textBox1.append("Attempting to reconnect...
");
if (!ConnectToValidator(10, 3))
{
// If it fails after 5 attempts, exit the loop
Running = false;
}
}
// tick the timer
// timer1.start();
// update form
UpdateUI();
// setup dynamic elements of win form once
if (!bFormSetup)
{
SetupFormLayout();
bFormSetup = true;
}
}
//close com port
Hopper.getM_eSSP().CloseComPort();
btnRun.setEnabled(true);
btnHalt.setEnabled(false);
}
>在MainLoop()函数中,while循环一直运行,直到“运行为真”问题是,如果我想停止while循环,则必须将Running设置为false,这是在另一个按钮btnHalt上完成的:
ActionListener btnHalt_Click = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textBox1.append("Poll loop stopped
");
System.out.println("Hoper Stopped");
Running = false;
}
};
但是btnHalt没有响应,整个帧被冻结,也没有
显示文本区域中的任何日志.