Building Coder-Failure Rollback (Failure Rollback)

Source: Internet
Author: User

Roll back a single failed operation in a continuous transaction and disable all failure message boxes (including warnings and errors)


Problem

I tried to implement a failed handler function: Disable all warning message boxes and revoke the actions that cause the warning. However, I cannot find a way to disable message boxes that are not warnings.

My problem scenarios are as follows:

Create multiple Revit elements in an external command. This process may fail for some reason. However, I do not want to bring up a dialog box for the user to choose whether to continue or exit. I want the program to ignore the part of the operation that causes the failure and continue the subsequent operation. However, I need to record all failures and let the user know what happened at the end of the entire external command.

Answer

Two special statements are required in the solution.
-Return FailureProcessingResult. ProceedWithRollBack in the failed processing function;
-Call failureHandlingOptions. SetClearAfterRollback (true) to set transaction failure handling );


First, define a failure handling class:
[Csharp]
Public class FailureHandler: IFailuresPreprocessor
{
Public string ErrorMessage {set; get ;}
Public string ErrorSeverity {set; get ;}

Public FailureHandler ()
{
ErrorMessage = "";
ErrorSeverity = "";
}

Public FailureProcessingResult PreprocessFailures (FailuresAccessor failuresAccessor)
{
IList <FailureMessageAccessor> failureMessages = failuresAccessor. GetFailureMessages ();

Foreach (FailureMessageAccessor failureMessageAccessor in failureMessages)
{
// We're just deleting all of the warning level
// Failures and rolling back any others

FailureDefinitionId id = failureMessageAccessor. GetFailureDefinitionId ();

Try
{
ErrorMessage = failureMessageAccessor. GetDescriptionText ();
}
Catch
{
ErrorMessage = "Unknown Error ";
}

Try
{
FailureSeverity failureSeverity = failureMessageAccessor. GetSeverity ();

ErrorSeverity = failureSeverity. ToString ();

If (failureSeverity = FailureSeverity. Warning)
{
// If the message is a warning, the message box is disabled.
FailuresAccessor. DeleteWarning (failureMessageAccessor );
}
Else
{
// If it is an error, cancel the operation that causes the error, but continue the entire transaction.
Return FailureProcessingResult. ProceedWithRollBack;
}
}
Catch
{
}
}
Return FailureProcessingResult. Continue;
}
}

Then, when creating a transaction, specify the failure handling settings as follows:
[Csharp]
Transaction transaction = new Transaction (doc );

FailureHandlingOptions failureHandlingOptions = transaction. GetFailureHandlingOptions ();
FailureHandler failureHandler = new FailureHandler ();
FailureHandlingOptions. SetFailuresPreprocessor (failureHandler );
// This sentence is the key
FailureHandlingOptions. SetClearAfterRollback (true );
Transaction. SetFailureHandlingOptions (failureHandlingOptions );
 
 
Transaction. Start ("Transaction Name ");
// Do something here that causes the error
//......
Transaction. Commit ();
 
 
// This Code simply uses a WinForm dialog box to display the failure records saved in the failed processing function.
// You can perform any processing on these failure records in actual applications.
If (failureHandler. ErrorMessage! = "")
{
System. Windows. Forms. MessageBox. Show (failureHandler. ErrorSeverity + "|" + failureHandler. ErrorMessage );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.