0
我試圖用Parallel.ForEach
轉換ForEach
。C#Parallel.ForEach,不工作
ForEach
,工作完好,並給出預期的結果。當我嘗試使用Parallel.ForEach
時,我得到一個List變量的錯誤,說它不可用。
我在做什麼錯?我在代碼中添加了註釋來解釋。
public class CalssTest
{
List<AssetDetails> assets;
AssetDetails asset;
private void loadData()
{
//Loading Assetdetails here
//Skipped here to keep the code short.
}
private void cmdValidate_Click(object sender, RoutedEventArgs e)
{
OrderManager om = new OrderManager();
using (ATXFunctions atx = new ATXFunctions())
{
//*****THIS WORKS *********//.
foreach (AssetDetails ad in assets)
{
ad.OrderEntry.OrderType= "A";
ad.OrderEntry.Portfolio = "John";
ad.OrderEntry.Quantity = 1000;
ad.Compliance.Result = om.RunCompliance(atx.AtxEngine, ad.StockEntry, ad.OrderEntry);
}
//*****THIS ERRORS OUT *********//.
// Error ::
//The name 'assets' does not exist in the current context
Parallel.ForEach (assets (ad) =>
{
ad.OrderEntry.OrderType = "A";
ad.OrderEntry.Customer = "John";
ad.OrderEntry.Quantity = 1000;
ad.Compliance.Result = om.ValidateOrder(ad.StockEntry, ad.OrderEntry);
});
}
}
}
缺少逗號? – Jon
是的。我是一個白癡。在這個浪費的時間。 :)感謝您的迴應。 – teddy2