I've build the KeePass OneDriveSync plugin. I had someone point me to the fact that when they exit KeePass with still having changes and without having saved them yet, that my plugin doesn't complete successfully before KeePass actually shuts down. I did some research and I could easily reproduce the issue. I found out that KeePass only waits for the main UI thread to be done and then shuts down. It does not detect subthreads as I'm using for the async communication with OneDrive to be done before it exits. I hate to rewrite my plugin to do everything on the UI thread as that's really not nice. Is there a way to deal with this? I.e. notify the main thread that it's ready to end once the subthreads are done? Or could this be fixed in KeePass to also watch for subthreads before exiting?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I managed to build in a delay in the Terminate() method to stall KeePass from closing:
// Check if there's still a process running that we need to wait for before allowing KeePass to terminateif(IsSomethingStillRunning){Host.MainWindow.SetStatusEx("Waiting for OneDriveSync to complete...");do{Application.DoEvents();}while(IsSomethingStillRunning);}
However when I do this and my plugin does its sync thing in the background, once it gets to the point of doing the ImportUtil.Import, the Host.Database.IOConnectionInfo.Path is an empty string and Host.Database.IsOpen == false. So in Terminate() it already closed down the database which prevents me from being able to do an import on it still.
Any ideas or tricks to deal with this? I would need some event that signals me that the database is closing.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Looks good. If your threads require more than a few milliseconds to finish, I'd add a Thread.Sleep(1) in the loop, in order to avoid excessive CPU usage.
MainForm has three events for database closing: FileClosingPre, FileClosingPost and FileClosed. The first two events are raised before the database is being closed (before and after saving), and the third one after the database has been closed.
Best regards,
Dominik
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've build the KeePass OneDriveSync plugin. I had someone point me to the fact that when they exit KeePass with still having changes and without having saved them yet, that my plugin doesn't complete successfully before KeePass actually shuts down. I did some research and I could easily reproduce the issue. I found out that KeePass only waits for the main UI thread to be done and then shuts down. It does not detect subthreads as I'm using for the async communication with OneDrive to be done before it exits. I hate to rewrite my plugin to do everything on the UI thread as that's really not nice. Is there a way to deal with this? I.e. notify the main thread that it's ready to end once the subthreads are done? Or could this be fixed in KeePass to also watch for subthreads before exiting?
My suggestion would be that you wait for your subthreads to be terminated in the
Terminatemethod of your plugin.Best regards,
Dominik
Thanks for the suggestion! I'll give it a try.
I managed to build in a delay in the Terminate() method to stall KeePass from closing:
However when I do this and my plugin does its sync thing in the background, once it gets to the point of doing the ImportUtil.Import, the Host.Database.IOConnectionInfo.Path is an empty string and Host.Database.IsOpen == false. So in Terminate() it already closed down the database which prevents me from being able to do an import on it still.
Any ideas or tricks to deal with this? I would need some event that signals me that the database is closing.
Looks good. If your threads require more than a few milliseconds to finish, I'd add a
Thread.Sleep(1)in the loop, in order to avoid excessive CPU usage.MainFormhas three events for database closing:FileClosingPre,FileClosingPostandFileClosed. The first two events are raised before the database is being closed (before and after saving), and the third one after the database has been closed.Best regards,
Dominik
FileClosingPre sounds exactly what I need! Thanks for the pointer. I'll try it out.
Resolved! Thanks again!