3 September 2008

How to: Use nested commands

A new subsequent command can be started while another parent command has already been started:


using (UndoRedoManager.Start("My Root Command"))
{
using (UndoRedoManager.Start("My Nested Command"))
{
...
}
...
}



You can put part of a job inside the nested command and then cancel it if error occured without canceling parent command.

Commands hierarchy can be of any depth. Some of commands can be commited and some of them can be rolled back. All the changes made in these commands will be put in the history when root command commited.

So, despite the number of nested commands you will see the only one root command in history containing all changes accumulated.

2 comments:

Anonymous said...

This doesn't work for me. When I try the second "using", it throws an InvalidOperationException, saying:

"Previous command is not completed. Use UndoRedoManager.Commit() to complete current command."

I am using a version downloaded from CodePlex, "Beta 1", from the "Sources" download, and using the "Release" dll within that.

Samus said...

I also get the error message:

"Previous command is not completed. Use UndoRedoManager.Commit() to complete current command."

Any word on this?