调查使用DependentClone创建DependentTransaction可能很值得。
“从属事务是一种事务,其结果取决于从中克隆它的事务的结果。”
“DependentTransaction 是使用 DependentClone 方法创建的 Transaction 对象的克隆。它的唯一目的是允许应用程序停止并保证在事务仍在执行工作时事务不能提交(例如,在工作线程)。”
编辑:刚刚在相关的 SO 问题列表中发现了这一点:相关问题和答案请参阅他们提供的 msdn 链接:非常值得一读使用 DependentTransaction 管理并发
取自上面的 MSDN 链接(为简洁起见):
public class WorkerThread
{
public void DoWork(DependentTransaction dependentTransaction)
{
Thread thread = new Thread(ThreadMethod);
thread.Start(dependentTransaction);
}
public void ThreadMethod(object transaction)
{
DependentTransaction dependentTransaction = transaction as DependentTransaction;
Debug.Assert(dependentTransaction != null);
try
{
using(TransactionScope ts = new TransactionScope(dependentTransaction))
{
/* Perform transactional work here */
ts.Complete();
}
}
finally
{
dependentTransaction.Complete();
dependentTransaction.Dispose();
}
}
//Client code
using(TransactionScope scope = new TransactionScope())
{
Transaction currentTransaction = Transaction.Current;
DependentTransaction dependentTransaction;
dependentTransaction = currentTransaction.DependentClone(DependentCloneOption.BlockCommitUntilComplete);
WorkerThread workerThread = new WorkerThread();
workerThread.DoWork(dependentTransaction);
/* Do some transactional work here, then: */
scope.Complete();
}