msdn link: http://msdn.microsoft.com/zh-cn/library/w97s6fw4 (V = vs.80). aspx
Class program {static void main (string [] ARGs) {try {using (transactionscope = new transactionscope () {customenlistment cstomenlistment = new customenlistment (); cstomenlistment. preparing + = new customenlistment. preparingeventhandler () => {testclass. num1 = 1; testclass. num2 = 2; testclass. num3 = 3; throw new exception ("Throw test error! ") ;}); Cstomenlistment. rollbacking + = new customenlistment. rollbackingeventhandler () => {testclass. num1 = 0; testclass. num2 = 0; testclass. num3 = 0 ;}); transaction. current. enlistvolatile (cstomenlistment, enlistmentoptions. none); guid = new GUID ("{3456789a-7654-2345-abcd-098765434567}"); transaction. current. enlistdurable (guid, cstomenlistment, enlistmentoptions. none); transactionscope. compl Ete () ;}} catch {console. writeline ("Show ERROR infomation! ");} Thread. sleep (1000); // The transaction starts asynchronous thread execution independently to stop the main thread and is used to test the effect console. writeline (testclass. num1.tostring (); console. writeline (testclass. num2.tostring (); console. writeline (testclass. num3.tostring (); console. read () ;}}// two-phase commit (2 PC) public class customenlistment: ienlistmentnotification {Public Delegate void preparingeventhandler (); public event preparingeventhandler preparing; Public Delegate void rollbacki Ngeventhandler (); public event rollbackingeventhandler rollbacking; // check whether each group of transactions executes public void prepare (preparingenlistment) {try {preparing (); preparingenlistment. prepared (); // call commit} catch {preparingenlistment. forcerollback (); // call rollback} // submit public void commit (enlistment) {enlistment. done ();} // roll back public void rollback (enlistment) {rollbackin G (); enlistment. Done () ;}// the description of the method comes from msdn // The indoubt method should be implemented for the volatile resource manager. // Call this method when the transaction manager loses contact with one or more participants (so its status is unknown. // If this happens, you should record this fact whether or not any transaction participants are in an inconsistent state for future investigations. Public void indoubt (enlistment) {enlistment. done () ;}//< summary> /// test class /// </Summary> public class testclass {Private Static int num1 = 0; public static int num1 {get {return testclass. num1;} set {testclass. num1 = value ;}} Private Static int num2 = 0; public static int num2 {get {return testclass. num2;} set {testclass. num2 = value ;}} Private Static int num3 = 0; public static int num3 {get {return testclass. num3;} set {testclass. num3 = value ;}}}
if you have any doubts or errors, you are welcome to correct and discuss them.