I have problem while working with JFrame
, which get freezes while
running the code continuously. Below is my code:
On clicking on
btnRun
, I called the functionMainLoop()
:ActionListener btnRun_Click = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { MainLoop(); } };
Implementation of
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); }
In the
MainLoop()
function, the while loop is running continuesly until the Running is true problem is that if i want to stop that while loop i have to set Running to false which is done at another buttonbtnHalt
:ActionListener btnHalt_Click = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textBox1.append("Poll loop stopped "); System.out.println("Hoper Stopped"); Running = false; } };
but btnHalt
is not responding, whole frame is get freeze, also not
showing any log in the textarea
.