Sunday 15 March 2015

swing - Java JButton actionPerformed freezing -


I am fooling around with Java and I was trying to create a program that started pinging an address Gives "MS"

I have a pocket:

  pocket start = new pocket ("start"); Start.addActionListener {Public Zero Action Action (Action Event E) {try {doCommand (}}} Catch (IOE EXPRESS E1) {// TODO Automatic Generated Grip Block e1.printStackTrace ();}} });   

and doCommand () method looks like this:

  public static zero doCommand () throws IOException {string s = null; Procibbler PB = new process builder (commands); // Definition processbilder process process = pb.start (); // zaženemo proces (vrne process) BufferedReader stdInput = new BufferedReader (new InputStreamReader (proces.getInputStream ())); // Bronze Output Processa buffed reader stdError = new BufferedReader (New InputStreamReader (proces.getErrorStream ())); // Branje error outputa while ((s = stdInput.readLine ()) = = null {// dokler output obstaja (ni error) int dvop = s.indexOf (":") + 16; If (s.startsWith ("Answer")) {s = s.sstring (dvop); Int pres = s.indexOf (""); S = s.sbrstring (0, Pres-2); //System.out.println(s); Label.setText (s); }} While ((s = stdError.readLine ())! = Null) {// Docher error barrier system. Outprintline (s); }}   

What happens, every time I press the button, the program still freezes and nothing happens, I can also close it in the "normal" way ... It seems that I'm doing something wrong ...

An extension on StinePike's answer. What he actually says, the ActionPerford () method will be running on the main GUI event thread, which means that the GUI can respond only when the action-affected () returns. The code you posted uses Blocking I / O which can not be completed in a fast way.

Specifically, this line:

  while ((s = stdInput.readLine ())! = Null} {  

stdin Which block on the input from Although this block effectively becomes the GUI unresponsive because the action-oriented () method of the method has not returned yet.

Since you have said that the purpose of this code is to return the response from an external application, it may be that in the external application either: something returned on stderr or blocked on any other condition.

A possible solution is as follows:

doCommand () method:

  public static zero doCommand () throws IOException {String s = null; Procibbler PB = new process builder (commands); // Definition processbilder process process = pb.start (); // zaženemo proces (vrne process) final BufferedReader stdInput = new BufferedReader (new InputStreamReader (proces.getInputStream ())); // Brands Output Processes Last BufferedReader stdError = New BufferedReader (New InputStreamReader (proces.getErrorStream ())); // Branje Error Output Thread Reid STIN = New Thread (New Runnabal) {Public Zero Run} {try {while ((s = stdInput.readLine ()) = null {// Dockler Output Dinda (ni error) int Dvop (S): " (0, Presse2); // System.out.printlean (S); // Perform on main AWT thread (I'm assuming 'label' is the name of one of your GUI components) SwingUtilities.invokeLater (new Runnable {} Public Zero Run () {label.setText (s);}}}}}}} String (iOException ex) {// handle}}}; thread readStdErr = new thread (new run) (public went from zero) {{({s = stdError.readLine ()) = null} {// DokLar error barrier system. Out.printline (s);} hold (IOException pre) {// handle this many}}}}; readStdIn.start (); readStdErr.start ();}   

The above code will create two different threads in which the contents of stdin and stderr will be read and processed individually, since the main GUI thread has been blocked for Block I / O, The GUI should be responsive, even if the external application does some strange (like freeze or deadlock).

Note: Even after the button is pressed, even if the external application is not finished.

Edit: BufferedReader.readLine ()

No comments:

Post a Comment