/// <summary> /// Starts a background task that will update, add or delete the /// SrsItem according to the given operation type. /// </summary> /// <param name="operationType">Operation to execute on the SRS /// item.</param> private void SendEntity(SrsEntryOperationEnum operationType) { BackgroundWorker sendEntityWorker = new BackgroundWorker(); sendEntityWorker.DoWork += DoSendEntity; sendEntityWorker.RunWorkerCompleted += DoneSendEntity; sendEntityWorker.RunWorkerAsync(operationType); }
/// <summary> /// Background task work method. /// Sends the entity to the database. /// </summary> private void DoSendEntity(object sender, DoWorkEventArgs e) { SrsEntryOperationEnum operationType = (SrsEntryOperationEnum)e.Argument; // Acquire the send object. Thus, only one send operation can occur // at any given time. lock (_sendLock) { // Once the lock is acquired, check that the work is not done. // You wouldn't want to submit the item again if it worked // the first time. if (!_isDone) { // The work has still not been done. // Set the IsSending value and start the job. IsSending = true; if (operationType == SrsEntryOperationEnum.Update) { // Update try { _isDone = _srsEntryDao.Update(_entry.Reference); if (!_isDone) { ErrorMessage = R.SrsItem_EditFailure; } } catch (Exception ex) { // An exception occured. // Log the exception and set the error message. LogHelper.GetLogger(this.GetType().Name) .Error("Could not update the entity.", ex); ErrorMessage = R.SrsItem_EditFailure; } } else if (operationType == SrsEntryOperationEnum.Add) { // Add try { // Sets some properties Entry.Reference.CreationDate = DateTime.UtcNow; // Add the entity to the database. _srsEntryDao.Add(_entry.Reference); _isDone = true; // Saves the value of the tags Properties.Settings.Default.LastSrsTagsValue = Entry.Tags; } catch (Exception ex) { // An exception occured. // Log the exception and set the error message. LogHelper.GetLogger(this.GetType().Name) .Error("Could not add the entity.", ex); ErrorMessage = R.SrsItem_EditFailure; } } else { // Delete try { _isDone = _srsEntryDao.Delete(_entry.Reference); if (!_isDone) { ErrorMessage = R.SrsItem_EditFailure; } } catch (Exception ex) { // An exception occured. // Log the exception and set the error message. LogHelper.GetLogger(this.GetType().Name) .Error("Could not delete the entity.", ex); ErrorMessage = R.SrsItem_EditFailure; } } // After the job. if (_isDone && FinishedEditing != null) { // If the job has been done, raise the event. ExtendedSrsEntry result = (operationType == SrsEntryOperationEnum.Delete ? null : Entry); FinishedEditing(this, new SrsEntryEditedEventArgs(result, true)); } IsSending = false; } } }