TransactionScope::Dispose Method ()
Ends the transaction scope.
Assembly: System.Transactions (in System.Transactions.dll)
Calling this method marks the end of the transaction scope. If the TransactionScope object created the transaction and Complete was called on the scope, the TransactionScope object attempts to commit the transaction when this method is called.
The use of the C# using construction ensures that this method is called even if an exception occurs. Exceptions that occur after calling this method may not affect the transaction. This method also restores the ambient transaction to it original state. A TransactionAbortedException is thrown if the transaction is not actually committed.
This method is synchronous and blocks until the transaction has been committed or aborted. Because of this, you should be extremely careful when using this method in a Windows Form (WinForm) application, or a deadlock can occur. If you call this method inside one WinForm Control event (for example, clicking a button), and use the synchronous Invoke method to direct the control to perform some UI tasks (for example, changing colors) in the middle of processing the transaction, a deadlock will happen. This is because the Invoke method is synchronous and blocks the worker thread until the UI thread finishes its job. However, in our scenario, the UI thread is also waiting for the worker thread to commit the transaction. The result is that none is able to proceed and the scope waits indefinitely for the Commit to finish. You should use BeginInvoke rather than Invoke wherever possible, because it is asynchronous and thus less prone to deadlock.
For more information on how this method is used, see the Implementing An Implicit Transaction Using Transaction Scope topic.
The following example demonstrates how to use the TransactionScope class to define a block of code to participate in a transaction.
Available since 2.0
TransactionScope Class
System.Transactions Namespace
Implementing An Implicit Transaction Using Transaction Scope