SWT TextBox Write is much slower then C++ executable prints - java

I am creating a Java GUI which interacts with C++ executable using ProcessBuilder. All the InputStream, OutputStream, ErrorStream from the C++ executable are redirected to the GUI TextBox. The C++ executable is very fast and it outputs lots of messages. Now the problem is eventhough the C++ executable is completed execution, GUI is still printing those messages over TextBox (as I am creating Display thread to write into GUI TextBox) for another 5-6 Minutes. Is there any way I can sync GUI-TextBox write speed with that of C++ executable prints? Thanks in Advance.

One idea see if it works for you:
Don't try to refresh the gui with every messages coming from c++. Try to buffer them and minimize the number of callbacks you do for GUI refresh. You cannot decide upfront a good buffer size that you can flush to GUI. You may have to do this fine tuning by trying different buffer sizes and limiting the number of GUI refresh through callback.
Hope it helps!

Related

Implement a Execution Log in Java

Coming from a WPF world, I am having a hard time implementing some basic stuff in Java.
I need to execute a *.JAR file from another java GUI application. This JAR file takes minutes to finish, and I want the GUI not to freeze.
So, my initial solution would be: create a SwingWorker class to call a command line in background.
My main concerns are:
I need to get the console output from the *.JAR file and show it in the GUI. Is it possible to update the GUI from a background thread?
Is it possible to cancel the thread from a user input?
How do I get notified that the thread is finished?
Is my solution a good one?

Can Matlab deploytool compile files with javax.swing elements?

Suppose I have a set of codes to display a JFrame, a JPanel, and a JLabel. This works fine if I run it as a script file. It just shows a tiny window with a label that says "A label" exactly like you would expect:
frame = javax.swing.JFrame('Test');
panel = javax.swing.JPanel();
label = javax.swing.JLabel('A label');
panel.add(label);
frame.add(panel);
frame.setDefaultCloseOperation(javax.swing.JFrame.HIDE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
The problem comes when I compile this as an exe file with the deploytool. It will compile and I can run the program, but the frame will show up for about 3 seconds or so then disappear. If I run from inside Matlab with !main.exe, there is no error message when the window disappears (I don't want to say it crashes because there is no error message). Neither is there one if I run the executable from the Windows command prompt (same results -- shows for a few seconds and then crashes).
Any ideas what is going on here? I can compile other files just fine. Is the problem because I included the javax.swing elements?
Many thanks for your help.
UPDATE
This feels like a really cheap hack, but having a while loop that pauses Matlab as long as the JFrame is open does the trick. So now the question is, is there a better way to do this?
The problem is probably that your main M-code function finishes executing, and since there are no figures up, Matlab decides to exit. In a Java Swing program, what would happen is that things would keep going until all the Swing windows are closed or you explicitly terminate the program. Since this is a Matlab program, the layer that's "in control" is the Matlab handle graphics layer, so you need to either have the main function executing or a figure up. (In interactive Matlab, it'll keep running as long as you have the IDE up, but there's no IDE in a compiled Matlab program, so when its work is done, it exits.)
The "Right Thing" to do from MathWorks' perspective is to probably buy the Matlab Builder JA toolbox, build the Matlab part of your program in to a Java library, include that in a main program which you write in Java. That way the Java layer is "in control" of the main execution sequence, and the "stay running as long as there are Java windows open" logic you're expecting will be in effect.
If you want to hack this to make it work in your current program structure, your invisible figure window might be a good one. Though you will need to make it Visible to have it work; invisible figures don't count for keeping a Matlab GUI running. You may be able to hide it from the user by changing its position to move it entirely off the user's screen.
Then you need to terminate the program somehow. Some part of your code is going to know when the program should end. That sounds like it's the Java part of your code. From there, you could just call java.lang.System.exit(). If you need to do Matlab-layer stuff, you could exit from M-code by communicate the "it's time to exit" back to your Matlab code, which could then call exit() or close that figure. You could do this by setting a public class variable in one of your Java classes, and have a Matlab timer object that checked that variable every 500 milliseconds or so.
If the condition that ends the program is that all your Java Swing windows get closed, that's a little tougher. Because the Matlab figure window itself is a Java AWT or Swing window, so as long as that's open you're not getting down to zero windows. What you could do is have that Matlab timer, instead of looking for a class variable, check the list of open Java windows and see if the Matlab figure is the only one left, and if so, close it or exit explicitly.

Automating interaction with a closed-sourced Windows XP GUI-based Program

I've a closed-sourced Windows XP GUI-based that performs some measurements.
My current manual workflow is:
Start the data capture by clicking on the "Run" button on the GUI
Stop the data capture by pressing the "Stop" button on the GUI.
Read some value displayed on the screen.
Save the data for the session to a file.
There is no library or API to automate this whole interaction and therefore I've no option but to do it manually :( and I'm sick of it !
As you clearly see that this approach is not only time-consuming but also error prone because it is limited by my reaction time which varies with every run.
I was wondering if there is a way to automate this interaction? If yes, what are my options? I would prefer to implement something in Python or Java. But I'm open to other options as well.
My idea is to implement a server process that runs on the Windows XP machine. I can then remotely send requests to this server process which in turn will execute my workflow automatically.
There's an amazing windows GUI automation tool called autoit. http://www.autoitscript.com/site/autoit/
You asked about linking AutoIt with Java. For my purposes, I've done this using a ProcessBuilder to create a Process, then get the Processes InputStream and ErrorStream and be sure to handle these streams in a separate thread. I have AutoIt communicate with my Java program using the InputStream. A good article for this (though a little out of date) is this one: When Runtime.exec() won't. It is key to be sure that the process be run on a background thread and that the two streams be read in their own threads. If you're doing this in a Swing GUI, then extra care must be taken that all Swing calls be made on the main Swing event thread, the EDT.

How to Remotely Block and Unblock any application on Windows

I am creating a program using Java Sockets in which I capture the client desktop and send messaging to client. Its working properly but now I want to block Client applications like Notepad, MS-Word, etc.
How can I do this?
Thanks.
It is hard to do using pure java API.
I do not know what do you mean when you say "block". The easiest way is to check from time to time running processes and kill one named "notepad" by executing taskkill from java.
If you wish to achieve effect of inactivity of application, i.e. user sees the notepad but cannot type you can do the following.
You have to check which application is on front. There is no clean pure java solution for this but you can probably write VBScript or JScript that does this task and run it from java. Once you detected that notepad is on top create transparent window (or even probably half-transparent window) that occupies full screen. Bring it on top. User will not be able to type into notepad because your window is on top but will see it.
Here is reference how to create transparent windows: http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/
Good luck.

Getting scp's status bar to appear in a Java window

I'm writing a program that uses scp to copy files in a bigger java program. As it stands now, the program freezes up while the scp is copying the file, which can take a few minutes, so I'd like to be able to display the progress of the scp or at the very least get the terminal window with the scp progress to show up! Any suggestions?
scp writes to STDOUT and flushes the output. If you pipe the output, you may be able to read the output from the pipe, but my guess is that 1) scp wont print the progress bar to a pipe or 2) if you can do 1, the output will only make sense if you re-implement a pty or similar in a java window.
However, you could experiment with an expect-like module calling scp and mimic how the terminal works and you may be able to fake scp into thinking its being run as a terminal.
Finally, (and this is probably the best answer), don't use scp, find a java library to import that directly links into openssh's protocols for transferring files with scp.
When you say, "the program freezes up", my guess is that you mean that a Swing UI freezes up. If so, launch scp in a background thread and show the user an indeterminate progress bar.
Invoke the "launch scp" using SwingWorker to get it of the main thread and let the GUI update again.
To get actual progress you will need to investigate the characters printed by scp. I believe it maintains an ASCII progress bar, you can then replicate in Java. You may want to add "-v" to scp to let it be more talkative.

Categories

Resources