我在使用JFrame
時出現問題,它會凍結,而 會不斷運行代碼。下面是我的代碼:JFrame凍結時連續運行代碼
在點擊
實施btnRun
,我調用的函數MainLoop()
:MainLoop()
ActionListener btnRun_Click = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { 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("\r\nPoll Loop\r\n" + "*********************************\r\n"); } // 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...\r\n"); 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()
功能,而循環繼續運行,直到運行是真實的問題是,如果我wa NT才能停止while循環我必須設置運行到這是在另一個按鈕btnHalt
做虛假:ActionListener btnHalt_Click = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textBox1.append("Poll loop stopped\r\n"); System.out.println("Hoper Stopped"); Running = false; } };
但btnHalt
沒有響應,整個畫面是得到凍結,也沒有 顯示任何日誌textarea
。
請學習java命名約定並堅持使用它們。 – kleopatra 2013-03-21 11:09:14