Zé *_*los 2 .net c# multithreading transactions transactionscope
我有一个类做一些事务代码.
让我们假设:
class Worker
{
public void doWork()
{
//I do not want to create a new transaction. Instead, i want to use the environmenttransaction used by the caller of this method
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required)) {
workItem1();
workItem2();
workItem3();
scope.Complete();
}
}
Run Code Online (Sandbox Code Playgroud)
现在我有一些执行此代码的线程:
Worker worker = new Worker();
using (TransactionScope transaction = new TransactionScope())
{
Thread Thread1 = new Thread(new ThreadStart(worker.doWork));
Thread1.Start();
Thread Thread2 = new Thread(new ThreadStart(worker.doWork));
Thread2.Start();
Thread Thread3 = new Thread(new ThreadStart(worker.doWork));
Thread3.Start();
Thread.Sleep(10000); //this should be enough to all the workers finish their job
transaction.Complete();
}
Run Code Online (Sandbox Code Playgroud)
每个线程都在创建自己的事务.如何在所有线程之间共享相同的事务?