user interface - How to set a Java Gui listener for unexpected program end -


i making personal planning program. utilize xml documents store user data , login data. not utilize server , accounts created in program localized computer stored on (in xml documents.)

in login xml document, keep track of users logged in 1 user can't have 2 windows @ same time prevent conflicts data. feature runs smoothly , have no problem it.

the thing want know if there way catch unexpected shut down of program (such task-manager close or forced close when shutting down computer) can "log" user off of xml document. otherwise user never able on after unexpected program close without going xml document , deleting username logged in list.

it seems shutdown hook not work event queue java gui. this thread tried setting code shown , shutdown hook doesn't work me either. there suggestions ways of catching unexpected shutdown without shutdown hooks?

this code:

import java.awt.eventqueue;  public class gui {  private static controller controller;  public static void main (string[] args) {      controller = new controller();      runtime.getruntime().addshutdownhook(new thread() {             @override             public void run() {                 controller.savestate();                 controller.loguserout();             }             });      eventqueue.invokelater(             new runnable() {                 public void run() {                     controller.start();                 }             });     } } 

this closer @ controller logs user out

public void loguserout() {     logindatabase.loguserout(username);     saveloginstate(); } 

all logindatabase removes username list of logged in users user free log in again

public void saveloginstate() {     xstream xstream = new xstream(new domdriver());       outputstream outfile;     try {         string filepath = "data" + file.separator + "logindatabase.xml";         outfile = new bufferedoutputstream(new fileoutputstream(filepath));         xstream.toxml(logindatabase, outfile); // writes state outputfile;         outfile.close(); //close writer     } catch (filenotfoundexception e) {         e.printstacktrace();     } catch (ioexception e) {         e.printstacktrace();     } } 

this process writing on login state xml file. suspect might long shutdown hook if being called expect.

any suggestions? thought long time possibly using simple variables solve problem because have program set user can logged multiple accounts, use of variables impossible.

also, controller object contained in scope of shutdown hook same controller modified in event queue scope?

the shutdown hook can solution here. details see e.g. answer: https://stackoverflow.com/a/2541618/2045440

[off topic] however, if you're @ risk unexpected termination of application can result in lost of important data, maybe worth consider more persistent way of processing data (autosaving, backup files etc).


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -