0
mainFrame().addWindowListener(new java.awt.event.WindowAdapter() {
    @Override
    public void windowClosing(java.awt.event.WindowEvent evt)
    {
        retrieveItems();
        closeAllConnections();
        System.exit(0);
    }
});

For the code above, retrieveItems() executes nicely but there is a Background Task called doBackup() that's inside closeAllConnections() which does not execute at all. The program just seems to skip the task. Here is a brief view of what closeAllConnections() contains;

public void closeAllConnections()
{
    boolean shouldBackup = getBackupOnCloseStatus();
    if(shouldBackup)
    {
        doBackUp();
    }
    dbManager.disconnectDB();
}

After using the Netbeans Step-Debugger, I noticed that the program execution does not even enter the doBackup() method before application exits. This is strange. I would highly appreciate any help as regards what might be causing this behaviour. Thanks good people!

Just if this piece of extra info might be helpful in finding a solution, I'm using Swing Application Framework, so the the Task doBackup() looks like the following...

@Action
public Task doBackUp()
{
    return new DoBackUpTask(getApplication());
}

private class DoBackUpTask extends org.jdesktop.application.Task<Object, Void> 
{
    private boolean done = false;

    DoBackUpTask(org.jdesktop.application.Application app) 
    {
        super(app);
        this.setMessage("Backing-up database...");
    }

    @Override 
    protected Object doInBackground() 
    {
        File destDir = new File(appManager.getBackUpDir());
        done = backUpDataBase(destDir);
        return null;
    }

    @Override 
    protected void succeeded(Object result) 
    {
        if(done)
        {
            setMessage("BackUp completed.");
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
SourceVisor
  • 1,868
  • 1
  • 27
  • 43
  • 2
    Do you set the JFrame's default close operation anywhere? – Hovercraft Full Of Eels Mar 20 '13 at 03:43
  • 1
    I would say is because you call `System.exit(0)` before the `DoBackUpTask` can executed (assuming that it's a non-daemon thread) – MadProgrammer Mar 20 '13 at 03:47
  • @HovercraftFullOfEels i didin't explicitly set the default close operation... But i suspect that the Netbeans GUI-Builder might be calling System.exit(0) when the application's mainFrame ('FrameView' in the case of SAF) is closed using the 'X' button. – SourceVisor Mar 20 '13 at 04:12
  • @MadProgrammer, I think you may be right about that one, but I haven't been able to find where the netbeans GUI-Builder sets close operation on the main FrameView. Its sort of easier to find on JDialogs than on these SAF FrameViews – SourceVisor Mar 20 '13 at 04:17
  • @Teejay Perhaps you can use a [shut down hook](http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29) instead – MadProgrammer Mar 20 '13 at 04:20

2 Answers2

0

This may not be the best answer, but I have noticed that when calling System.exit(0) sometimes it will cut everything off. I was having a problem once when calling a method that depended on results from the previous line. All should be well, but it didn't work right because it was getting to the execution of the next line before the previous line was finished. I'm sure someone can talk more about the event dispatch thread and why this seemed to happen even though everything about computer science says it shouldn't. But here's my thought: Add a boolean return value to closeAllConnections to return true when complete, then re-write your window listener like this:

    mainFrame().addWindowListener(new java.awt.event.WindowAdapter() {
    @Override
    public void windowClosing(java.awt.event.WindowEvent evt)
    {
        boolean isClosed = false;
        while(!isClosed){
            retrieveItems();
            isClosed = closeAllConnections();
        }    
        System.exit(0);
    }
});

Maybe this will help.

Chandra Sekhar
  • 16,256
  • 10
  • 67
  • 90
Lucullan
  • 1
  • 2
  • Thanks for this piece of help... I kind of wouldn't prefer the looping thing though. But this might prove very useful as a last resort. – SourceVisor Mar 20 '13 at 04:21
0

My guess would be that you have another WindowListener elsewhere that's calling System.exit() or else you're doing a JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE), either of which will cause the app to terminate immediately, regardless of whatever other threads are executing.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • As much as I am convinced that you're very right, I have rechecked my code to ensure that `System.exit()` isn't called on any other `WindowListener` but I haven't been able to discover where to disable `JFrame.EXIT_ON_CLOSE` on my `FrameView` created with NetBeans GUI-Builder. I probably will, when I'm prepared to... but do u have any quick ideas? – SourceVisor Mar 22 '13 at 13:50