2010-08-20 61 views
4

我有一個TransactionScope對象,我想將它用於使用Parallel.ForEach創建的所有任務,我該如何實現這一點?如何通過一組並行任務共享事務

我想編寫並行消息隊列,20-50的消息,消息隊列是事務性的:

using (var queue = new MessageQueue(_exportEndpoint)) 
{ 
     var label = string.Format("{0} ComponentId - {1}", DateTime.Now.ToUniversalTime(), componentId); 
     queue.Send(contents, label, MessageQueueTransactionType.Automatic); 
     _log.WriteInfo("ExportToQueue: Message sent to queue - " + label); 
} 

和主線程使用一個TransactionScope對象,我嘗試以下,但我得到一個超時交易提交:

var clone = Transaction.Current.DependentClone(DependentCloneOption.RollbackIfNotComplete); 
Parallel.ForEach(components.ToList(), c => ExportComponent(c, clone)); 
+0

你嘗試過什麼到目前爲止一個Complete?你有一些你想要完成的代碼示例嗎? – 2010-08-20 15:32:01

回答

0

只要這些任務單獨參與事務上下文,它就是透明的。換句話說,用事務範圍包裝你的foreach,你就完成了。

+0

並行任務正在嘗試寫入事務性消息隊列,我需要使用事務初始化線程以實現此目的 – AwkwardCoder 2010-08-20 15:33:35

相關問題