/// <summary> /// Reopens the connection to the database server asynchronously. /// </summary> /// <param name="callback">The callback method.</param> /// <param name="userState">The user state.</param> /// <returns>The asynchronous result.</returns> public override IAsyncResult Reopen(DbServerCallback callback, object userState = null) { // Create a new asynchrounous state for this operation. DbServerAsyncState asyncState = new DbServerAsyncState(this, userState); // Begin open the connection asynchronously on the thread pool. ThreadPool.QueueUserWorkItem((object state) => { // Execute asynchronously on the thread pool. try { // Reopen the connection. this.Reopen(); } catch (Exception exception) { // If an exception occurs, set the callback exception. asyncState.Exception = new DbException("Reopening the connection to the database server \'{0}\' failed.".FormatWith(this.Name), exception); } // Complete the asynchronous operation. asyncState.Complete(); // Call the callback method with the given state. if (callback != null) callback(asyncState); }); return asyncState; }
/// <summary> /// A callback function for when the removal operation completed, either successfully or unsuccessfully. /// </summary> /// <param name="state">The asynchronous state.</param> private void OnRemoveComplete(DbServerAsyncState state) { // Execute the code on the UI thread. this.Invoke(() => { // Hide the message. this.HideMessage(); // If the exception is not null, display an error message to the user. if (state.Exception != null) { MessageBox.Show( this, state.Exception.Message, "Database Server Removal Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } }); }
/// <summary> /// A callback method called when a disconnection to a server has completed. /// </summary> /// <param name="asyncState">The asynchronous state.</param> private void OnDisconnected(DbServerAsyncState asyncState) { // Execute the code on the UI thread. this.Invoke(() => { // Hide the connecting message. this.HideMessage(); // Check if an exception occurred. if (asyncState.Exception != null) { // If this a database exception. if (asyncState.Exception.IsDb) { // Display a database error message. MessageBox.Show( this, "Connecting to the database server \'{0}\' failed. {1}".FormatWith(asyncState.Server.Name, asyncState.Exception.DbMessage), "Connecting to Database Failed", MessageBoxButtons.OK, MessageBoxIcon.Error ); } else { // Display a generic error message. MessageBox.Show( this, "Connecting to the database server \'{0}\' failed. {1}".FormatWith(asyncState.Server.Name, asyncState.Exception.Message), "Connecting to Database Failed", MessageBoxButtons.OK, MessageBoxIcon.Error ); } } // Else, do nothing. }); }
/// <summary> /// A callback method called when changing the password of a server completed. /// </summary> /// <param name="asyncState"></param> private void OnPasswordChangeCompleted(DbServerAsyncState asyncState) { // Execute the code on the UI thread. this.Invoke(() => { // Hide the connecting message. this.HideMessage(); // Check if an exception occurred. if (asyncState.Exception != null) { // If this a database exception. if (asyncState.Exception.IsDb) { // Display a database error message. MessageBox.Show( this, "Connecting to the database server \'{0}\' failed. {1}".FormatWith(asyncState.Server.Name, asyncState.Exception.DbMessage), "Connecting to Database Failed", MessageBoxButtons.OK, MessageBoxIcon.Error ); } else { // Display a generic error message. MessageBox.Show( this, "Connecting to the database server \'{0}\' failed. {1}".FormatWith(asyncState.Server.Name, asyncState.Exception.Message), "Connecting to Database Failed", MessageBoxButtons.OK, MessageBoxIcon.Error ); } } // Else, show a notification message. else { MessageBox.Show( this, "The database server password has been changed successfully.", "Server Password Changed", MessageBoxButtons.OK, MessageBoxIcon.Information); } }); }
/// <summary> /// A callback method called when a connection to a server has completed. /// </summary> /// <param name="asyncState">The asynchronous state.</param> private void OnConnected(DbServerAsyncState asyncState) { // Execute the code on the UI thread. this.Invoke(() => { // Enable the tool strip. this.toolStrip.Enabled = true; // Hide the connecting message. this.HideMessage(); // Check if an exception occurred. if (asyncState.Exception != null) { // If this a database exception. if (asyncState.Exception.IsDb) { // Check the error type. switch (asyncState.Exception.DbType) { case DbException.Type.LoginPasswordExpired: if (DialogResult.Yes == MessageBox.Show( this, "The login password for the database server \'{0}\' has expired. Do you wish to change the password now?".FormatWith(asyncState.Server.Name), "Login Password Expired", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2 )) { // Change password. this.OnChangePassword(null, null); } break; case DbException.Type.LoginPasswordMustChange: if (DialogResult.Yes == MessageBox.Show( this, "To connect to the database server \'{0}\' you must change the password before the first login. Do you wish to change the password now?".FormatWith(asyncState.Server.Name), "Must Change Password", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2 )) { // Change password. this.OnChangePassword(null, null); } break; default: // Display an error message. MessageBox.Show( this, "Connecting to the database server \'{0}\' failed. {1}".FormatWith(asyncState.Server.Name, asyncState.Exception.DbMessage), "Connecting to Database Failed", MessageBoxButtons.OK, MessageBoxIcon.Error ); break; } } else { // Display an error message. MessageBox.Show( this, "Connecting to the database server \'{0}\' failed. {1}".FormatWith(asyncState.Server.Name, asyncState.Exception.Message), "Connecting to Database Failed", MessageBoxButtons.OK, MessageBoxIcon.Error ); } } // Else, process the user state. else { // If there exists a user asynchronous state. if (asyncState.AsyncState != null) { // If the user state is an event handler, call that event handler. if (asyncState.AsyncState is EventHandler) { // Get the event handler. EventHandler handler = asyncState.AsyncState as EventHandler; // Call the event handler. handler(this, EventArgs.Empty); } } } }); }