【发布时间】:2023-04-02 16:47:01
【问题描述】:
我正在尝试使用并行 For 循环来帮助我加快处理器密集型计算,然后将这些计算添加到线程安全列表中,在 for 循环完成后我可以访问该列表,以便我可以访问数据。我按照https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-parallel-for-loop-with-thread-local-variables的示例进行操作
我在粗体行中遇到编译时错误,由于我是多线程新手,如果您能指出我所犯的任何错误,我将不胜感激,以便我可以从这个错误中吸取教训。
public static async Task Test()
{
Vector<double> vectorArrayBuy = null;
Vector<double> vectorArraySell = null;
ConcurrentBag<DailyStockData> query;
query = new ConcurrentBag<DailyStockData>();
ConcurrentBag<MultipleRegressionInfo> listMRInfo = new ConcurrentBag<MultipleRegressionInfo>();
Calculations calcTemp = new Calculations();
**Parallel.For<ConcurrentBag<MultipleRegressionInfo>>(0, 200, (listMRInfo) = new ConcurrentBag<MultipleRegressionInfo>(), (j, loop, listMRInfoLocal) =>**
{
int k = Convert.ToInt32(j);
Calculations calc = new Calculations(query, k);
var targetValueBuy = calc.ListCalculationData.Select(i => i.MRTargetValueBuy).ToList();
var targetValueSell = calc.ListCalculationData.Select(i => i.MRTargetValueSell).ToList();
vectorArrayBuy = CreateVector.Dense(targetValueBuy.ToArray());
vectorArraySell = CreateVector.Dense(targetValueSell.ToArray());
var name = calc.ListCalculationData.First();
ConcurrentBag<double> value;
value = new ConcurrentBag<double>(calc.ListCalculationData.Select(i => i.WilliamsR));
MultipleRegressionInfo r1 = Rn(value, vectorArrayBuy, nameof(name.WilliamsR), k, calc);
listMRInfoLocal.Add(r1);
calcTemp = calc;
return listMRInfoLocal;
},
(variable) => listMRInfo = variable
);
listMRInfo = new ConcurrentBag<MultipleRegressionInfo>(listMRInfo.OrderByDescending(i => i.RSquared).DistinctBy(i => i.ValueName).ToList()); // trying to access this data after parallel for loop completes
public class DailyStockData
{
public DailyStockData();
public int ID { get; set; }
public string Symbol { get; set; }
public string Market { get; set; }
public DateTime Date { get; set; }
public decimal Open { get; set; }
public decimal High { get; set; }
public decimal Low { get; set; }
public decimal Close { get; set; }
public decimal AdjustedClose { get; set; }
public long Volume { get; set; }
}
public class CalculationData
{
public CalculationData(CalculationData calcData)
{
Date = calcData.Date;
Open = calcData.Open;
High = calcData.High;
Low = calcData.Low;
Close = calcData.Close;
AdjustedClose = calcData.AdjustedClose;
Volume = calcData.Volume;
WilliamsR = calcData.WilliamsR;
}
public CalculationData() { }
public DateTime Date { get; set; }
public double Open { get; set; }
public double High { get; set; }
public double Low { get; set; }
public double Close { get; set; }
public double AdjustedClose { get; set; }
public double Volume { get; set; }
public double WilliamsR { get; set; }
}
【问题讨论】:
-
这听起来像是编译时错误,而不是异常。如果您提供minimal reproducible example,会更容易为您提供帮助。
-
@JonSkeet 我添加了我使用的自定义类,但我不知道如何在不让你访问我的数据库的情况下让它为你运行。你有什么建议?
-
我建议您将其简化为 minimal 示例。我非常怀疑那里的大部分代码都与重现问题有关。
标签: c# multithreading parallel-processing task-parallel-library