【发布时间】:2017-03-09 17:19:54
【问题描述】:
我正在尝试确定事务是否已完成,根据结果,我想在另一个线程中做其他事情。
考虑下面的 TransactionScope:
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。
如何在 Action 中的每笔交易被处置之前跟踪其状态?
【问题讨论】:
-
你想达到什么目的?
-
@VMAtm 我只是想检查每个操作的事务是否仍在处理、中止或提交,然后再继续执行其他操作。我想我已经找到了解决办法。在下面检查我的答案。
标签: c# multithreading transactions transactionscope