public TransactionStatus RollBack()
Be aware that the returned status does not have to be necessarily the same like the status returned by GetStatus() even when the method is called immediately after rolling back the transaction. Such difference may happen due to actions made by a transaction finalizer, if there was one set. (See FailureHandlingOptions for more details.)
| Exception | Condition |
|---|---|
| InvalidOperationException | The current status of the transaction is not 'Started'. Transaction must be started before calling Commit or Rollback. -or- The transaction's document is currently in failure mode. No transaction operations are permitted until failure handling is finished. |
public bool CreateGrid(Autodesk.Revit.DB.Document document, XYZ p1, XYZ p2) { // All and any transaction should be enclosed in a 'using' // block or guarded within a try-catch-finally blocks // to guarantee that a transaction does not out-live its scope. using (Transaction transaction = new Transaction(document, "Creating Grid")) { // Must start a transaction to be able to modify a document if (TransactionStatus.Started == transaction.Start()) { // We create a line and use it as an argument to create a grid Line gridLine = Line.CreateBound(p1, p2); if ((null != gridLine) && (null != Grid.Create(document, gridLine))) { if (TransactionStatus.Committed == transaction.Commit()) { return true; } } // For we were unable to create the grid, we will roll the transaction back // (although on this simplified case we know there weren't any other changes) transaction.RollBack(); } } return false; }
Public Function CreateGrid(document As Autodesk.Revit.DB.Document, p1 As XYZ, p2 As XYZ) As Boolean ' All and any transaction should be enclosed in a 'using' ' block or guarded within a try-catch-finally blocks ' to guarantee that a transaction does not out-live its scope. Using transaction As New Transaction(document, "Creating Grid") ' Must start a transaction to be able to modify a document If TransactionStatus.Started = transaction.Start() Then ' We create a line and use it as an argument to create a grid Dim gridLine As Line = Line.CreateBound(p1, p2) If (gridLine IsNot Nothing) AndAlso (Grid.Create(document, gridLine) IsNot Nothing) Then If TransactionStatus.Committed = transaction.Commit() Then Return True End If End If ' For we were unable to create the grid, we will roll the transaction back ' (although on this simplified case we know there weren't any other changes) transaction.RollBack() End If End Using Return False End Function