C# 确定处置后交易的状态

C# 确定处置后交易的状态,c#,multithreading,transactions,transactionscope,C#,Multithreading,Transactions,Transactionscope,我试图确定事务是否已完成,并且根据结果,我希望在另一个线程中执行其他操作 考虑以下事务范围: using (TransactionScope scope = new TransactionScope()) { // Do stuff Scope.Complete(); } 现在,在另一个类中,在一个单独的线程中,我将浏览具有类似事务的对象列表: private static void ProcessActions() { while(true) {

我试图确定事务是否已完成,并且根据结果,我希望在另一个线程中执行其他操作

考虑以下事务范围:

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

    Scope.Complete();
}
现在,在另一个类中,在一个单独的线程中,我将浏览具有类似事务的对象列表:

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;
            }
        }
    }
}
在构造函数中创建新操作时,我为操作设置CurrentTransaction:

public Action()
{
    CurrentTransaction = System.Transactions.Transaction.Current;
}
问题是,当另一个线程处理操作时,action.CurrentTransaction被释放,抛出System.ObjectDisposedException


如何在处理操作中的每个事务之前跟踪其状态?

我相信我已经通过利用找到了解决方案

我使用以下代码为TransactionCompleted事件分配了一个委托:

System.Transactions.Transaction.Current.TransactionCompleted += new TransactionCompletedEventHandler(Mother.Current_TransactionCompleted);
在该方法中,我能够迭代我的操作,并确定哪个操作具有相应的原始事务。像这样:

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"
            }
        }
    }
}

我相信我已经找到了一个解决方法

我使用以下代码为TransactionCompleted事件分配了一个委托:

System.Transactions.Transaction.Current.TransactionCompleted += new TransactionCompletedEventHandler(Mother.Current_TransactionCompleted);
在该方法中,我能够迭代我的操作,并确定哪个操作具有相应的原始事务。像这样:

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"
            }
        }
    }
}

您想实现什么?@VMAtm我只是想在继续执行其他操作之前检查每个操作的事务是否仍在处理、中止或提交。我想我已经找到了解决办法。检查我下面的答案。你想实现什么?@VMAtm我只是想检查每个操作的事务是否仍在处理、中止或提交,然后再继续执行其他操作。我想我已经找到了解决办法。检查下面我的答案。