C# Parallel.ForEach,无法正常工作
C# Parallel.ForEach, not working
我正在尝试将 ForEach
转换为 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);
});
}
}
}
你的语法不正确使用:
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);
});
我正在尝试将 ForEach
转换为 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);
});
}
}
}
你的语法不正确使用:
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);
});