/// <summary> /// Do not allow the application to close if there are tasks running in the thread pool. /// Encrypt the database if it is marked for encryption before exitting the application. /// </summary> private void Fec_Main_FormClosing(object sender, FormClosingEventArgs e) { // Do not continue if application is exitting. if (exitting) { return; } if (jobCount > 0) { e.Cancel = true; MessageBoxAdv.Show(this, "There are database tasks running. \n" + "Please wait until they are completed. \n" + "If you close the application before the tasks are completed " + "the database may become corrupted.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // If there is a value stored in the password field of the database properties, then the database is to be encrypted. if (DatabaseProperties.password.Length > 0) { EncryptingDatabaseForm encryptingDatabaseForm = new EncryptingDatabaseForm(); encryptingDatabaseForm.Show(this); // Disable interaction with the main UI. this.Enabled = false; encryptionWorker.RunWorkerAsync(encryptingDatabaseForm); // Cancel form closing so that background worker can execute. e.Cancel = true; } }
/// <summary> /// Close the specified EncryptingDatabaseForm in a thread-safe manner. /// </summary> /// <param name="form"> The EncryptingDatabaseForm to close. </param> private void CloseEncryptingDatabaseForm(EncryptingDatabaseForm form) { if (form.InvokeRequired) { // Execute delegate. this.Invoke(new CloseEncryptingDatabaseFormDelegate(CloseEncryptingDatabaseForm), form); } else { form.Close(); } }