700 Software
700 Software

Reputation: 87773

Windows: signaling a Java process to show its window

I have a Java process that runs in the background. How can I quickly signal the process to show its window? I want a really light-weight script that can do this and can be launched from the Start Menu.

I think maybe a BAT file that checks if the lock file has been touched in the last few seconds, signal the process, otherwise, create a new one. The signal could be by creating another file that the process would be listening for.

That works, but it seems inefficient. Hesitation sounds unavoidable.

I could use Java instead of a BAT file, still that leaves the question of how to signal the background process. This only has to work in Windows, but I am good with Java so that is what I am using.

Any ideas?

Upvotes: 0

Views: 266

Answers (2)

Tom
Tom

Reputation: 19304

Is there anything preventing you from checking the lock file from your Java process? You could use the Observer pattern to alert the main thread (or which ever thread) to changes in the file.

For example:


public class FileWatcher implements Observable {

  private long lastModified;
  private final File file;

  public FileWatcher(File f) {
     this.file = f;
     this.lastModified = file.lastModified();

     Thread t = new Thread() {
        public void run() {
           while(!stopThread) {
           if(lastModified < file.lastModified()) {
              lastModified = file.lastModified();
              setChanged();
              notifyObservers();

           }    
           Thread.currentThread().sleep(5);      
           }
        }
     };

     t.start();
  }
}

DISCLAIMER: not tested or verified at all, but I'm sure you get the idea.

EDIT: oops, forgot the loop.

EDIT: new idea.

I have another idea. (I know you already accepted an answer, but I wanted to throw this out there.) Would it be possible to use the select function? In my very limited skim of the MSDN docs, this is only mentioned this in the context of sockets. I know the Linux equivalent is applicable to any file descriptor.

Instead of simply polling the file in the thread I mentioned above, let the OS do it! Pass the file into the writefds set to select and then it'll return when the file is modified. This means your process isn't spending valuable CPU time waiting for changes to the file.

I haven't verified whether or not Java exposes this call in the JDK, so it might require writing a JNI interface to get it to work. My knowledge in this area is a little fuzzy, sorry.

EDIT again again:

Found it! Java's Selector class looks like it implements select. Unfortunately, FileChannel isn't selectable, which is probably required in this case. :(

Upvotes: 1

BZ.
BZ.

Reputation: 1946

One option would be to have that process having a listener on a port (as an example 8888), then you could send a message to that port (or do something like telnet localhost 8888). The running processes could have a separate thread listening on that port.

Another option would be to use JMX communication with the JVM - see http://download.oracle.com/javase/6/docs/technotes/guides/management/agent.html

Upvotes: 3

Related Questions