2017-03-09 2 views
0

Ich versuche festzustellen, ob die Transaktion abgeschlossen wurde oder nicht, und je nach den Ergebnissen möchte ich andere Sachen in einem anderen Thread tun.Ermitteln des Status der Transaktion nach dem Verkauf

Betrachten Sie die unten Transaction:

using (TransactionScope scope = new TransactionScope()) 
{ 
    // Do stuff 

    Scope.Complete(); 
} 

nun in einer anderen Klasse, in einem separaten Thread, ich durch eine Liste von Objekten, werde die ähnliche Transaktionen:

private static void ProcessActions() 
{ 
    while(true) 
    { 
     action = pendingActions[0]; 
     if (action.CurrentTransaction == null || 
      action.CurrentTransaction.TransactionInformation.Status == TransactionStatus.Committed) 
     { 
      // Proceed to do things!! 
      remove = true; 
     } 
     else if (action.CurrentTransaction.TransactionInformation.Status == TransactionStatus.Aborted) 
     { 
      // Transaction has aborted. Remove this action from the list 
      remove = true; 
     } 

     if (remove) 
     { 
      lock (pendingActions) 
      { 
       pendingActions.Remove(action); 
       eventCount = pendingActions.Count; 
      } 
     } 
    } 
} 

stelle ich die CurrentTransaction für eine Aktion beim Erstellen einer neuen Aktion im Konstruktor:

Das Problem ist, dass zu dem Zeitpunkt, zu dem der andere Thread die Aktionen verarbeitet, action.CurrentTransaction entfernt wird und System.ObjectDisposedException ausgelöst wird.

Wie kann ich den Status für jede Transaktion in einer Aktion verfolgen, bevor sie gelöscht wird?

+0

Was möchten Sie erreichen? – VMAtm

+0

@VMAtm Ich wollte nur überprüfen, ob die Transaktion für jede Aktion noch verarbeitet, abgebrochen oder ausgeführt wird, bevor Sie mit anderen Dingen fortfahren. Ich denke, ich habe eine Lösung gefunden. Überprüfen Sie meine Antwort unten. –

Antwort

2

Ich glaube, ich habe eine Lösung gefunden mit Transaction.TransactionCompleted Event.

verwenden ich den folgenden Code einen Delegierten zu TransactionCompleted Ereignisse zuweisen:

System.Transactions.Transaction.Current.TransactionCompleted += new TransactionCompletedEventHandler(Mother.Current_TransactionCompleted); 

Bei diesem Verfahren kann ich meine Handlungen zu durchlaufen und bestimmen, welche die entsprechende Ursprung Transaktion hat. Wie folgt:

public static void Current_TransactionCompleted(object sender, TransactionEventArgs e) 
{ 
    var originatingTransaction = sender as System.Transactions.Transaction; 

    lock (pendingActions) 
    { 
     for (int i = pendingActions.Count - 1; i >= 0; i--) 
     { 
      var action = pendingActions[i]; 

      if (originatingTransaction.Equals(action.CurrentTransaction)) 
      { 
       var transactionStatus = e.Transaction.TransactionInformation.Status; 
       if (transactionStatus == TransactionStatus.Committed) 
       { 
        // Later in the code, I will do stuff if CurrentTransaction is null 
        action.CurrentTransaction = null; 
       } 
       else if (transactionStatus == TransactionStatus.Aborted) 
       { 
        // if It's aborted, I will remove this action 
        pendingActions.RemoveAt(i); 
       } 
       // I will skip processing any actions that still have a Transaction with the status of "Processing" 
      } 
     } 
    } 
} 
Verwandte Themen