2012-06-12 9 views
1

我嘗試用這個代碼:C#4.0的任務庫僵局

 foreach (var r in _vm.Rules.Take(20)) { 
      Task.Factory.StartNew(() => { 
       new SQLRuleSerializer().SaveRule(_vm.SelectedKey, r); 
      });    
     } 

錯誤:

Transaction (Process ID 600) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

我不知道這究竟是如何鎖死考慮到我沒有任何明確的鎖..我想問題是,序列化程序是一個代理,不能並行調用,即使通過單獨的實例?

回答

9

Transaction (Process ID 600) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

死鎖來自SQL Server,而不是C#代碼。

+0

ahhhhh ..好點。謝謝 –

1

您試圖並行保存20條不同的規則,但似乎每次保存都需要SQL服務器上的一些鎖。這意味着最有可能沒有並行增益,所以你應該使用普通循環而不使用TPL。