tags:

views:

388

answers:

1

Hai

I have a standalone application in which I have to prompt the user with an confirm dialog box to save the changes made by him when he tries to shutdown the system by start-->shutdown.

I came to know that by using signalhandlers we can do it.
Can some one help me how to use signal handlers

Thanking you
chaithu

+2  A: 

a ShutdownHook should be able to handle that case

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
    public void run() {
        // what you want to do
    }
}));

(with caveats)
See also:

as an illustration of simple signal handling:

public class Aaarggh {
public static void main(String[] args) throws Exception {
  Signal.handle(new Signal("INT"), new SignalHandler () {
    public void handle(Signal sig) {
      System.out.println(
        "Aaarggh, a user is trying to interrupt me!!");
      System.out.println(
        "(throw garlic at user, say `shoo, go away')");
    }
  });
  for(int i=0; i<100; i++) {
    Thread.sleep(1000);
    System.out.print('.');
  }
}
}
VonC