我有两个项目:一个类库和一个winform,所有计算都在类库中。现在,当用户单击“运行” botton时,我想为我的一个表单使用进度条。我尝试了下面的代码,但是在datagridview中加载数据(大约6000或更多)后,进度栏就会立即填充。
Winform项目中的代码:
private void BtnRun_Click(object sender, EventArgs e)
{
progressBar1.Maximum = TTools.Depth.Count;
progressBar1.Step = 1;
foreach (double doub in TLitho.CalcPercent().Item3)
{
progressBar1.PerformStep();
}
if (TTools.Depth != null)
{
dataGridView1.AllowUserToAddRows = false;
List<double> DolomitePer = TLitho.CalcPercent().Item3;
List<double> Depth = TTools.Depth;
var sourceD = new BindingSource
{
DataSource = Depth
};
for (int i = 0; i < (sourceD.Count); i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = sourceD[i];
}
var sourcePD = new BindingSource
{
DataSource = DolomitePer
};
for (int i = 0; i < (sourcePD.Count); i++)
{
dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[1].Value = sourcePD[i];
}
}
}
类库项目中的代码:
public static Tuple<List<double>, List<double>, List<double>> CalcPercent()
{
for (int j = 0; j < TTools.Depth.Count; j++)
{
MinimumIndexQC.Add(80000);
MinimumIndexQC[j] = lQC[j].IndexOf(lQC[j].Min());
MinimumIndexCD.Add(80000);
MinimumIndexCD[j] = lCD[j].IndexOf(lCD[j].Min());
MinimumIndexQD.Add(80000);
MinimumIndexQD[j] = lQD[j].IndexOf(lQD[j].Min());
CDandND.Add(Math.Abs(lCD[j][MinimumIndexCD[j]] - TPorosity.CalculateNDPorosity()[j]));
QCandND.Add(Math.Abs(lQC[j][MinimumIndexQC[j]] - TPorosity.CalculateNDPorosity()[j]));
QDandND.Add(Math.Abs(lQD[j][MinimumIndexQD[j]] - TPorosity.CalculateNDPorosity()[j]));
if (CDandND[j] < QCandND[j] && CDandND[j] < QDandND[j])
{
DistanceCD.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.9162) / Math.Sqrt(Math.Pow(0.0173, 2) + 1))
+ (Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.7031) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)));
DolomitePercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.7031) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)) / DistanceCD[j]);
CalcitePercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.9162) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)) / DistanceCD[j]);
QuartzPercent.Add(0);
DistanceQC.Add(0);
DistanceQD.Add(0);
}
else if (QCandND[j] < CDandND[j] && QCandND[j] < QDandND[j])
{
DistanceQC.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0192 * TPorosity.Neutron[j]) - 2.5818) / Math.Sqrt(Math.Pow(0.0192, 2) + 1))
+ (Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.7031) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)));
QuartzPercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.7031) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)) / DistanceQC[j]);
CalcitePercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0192 * TPorosity.Neutron[j]) - 2.5818) / Math.Sqrt(Math.Pow(0.0192, 2) + 1)) / DistanceQC[j]);
DolomitePercent.Add(0);
DistanceCD.Add(0);
DistanceQD.Add(0);
}
else if (QDandND[j] < CDandND[j] && QDandND[j] < QCandND[j])
{
DistanceQD.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0192 * TPorosity.Neutron[j]) - 2.5818) / Math.Sqrt(Math.Pow(0.0192, 2) + 1))
+ (Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.9162) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)));
QuartzPercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0173 * TPorosity.Neutron[j]) - 2.9162) / Math.Sqrt(Math.Pow(0.0173, 2) + 1)) / DistanceQD[j]);
DolomitePercent.Add((Math.Abs(TPorosity.BulkDensity[j] + (0.0192 * TPorosity.Neutron[j]) - 2.5818) / Math.Sqrt(Math.Pow(0.0192, 2) + 1)) / DistanceQD[j]);
CalcitePercent.Add(0);
DistanceCD.Add(0);
DistanceQC.Add(0);
}
else
{
DistanceCD.Add(0);
DistanceQC.Add(0);
DistanceQD.Add(0);
DolomitePercent.Add(0);
CalcitePercent.Add(0);
QuartzPercent.Add(0);
}
if (DolomitePercent[j]==0 && CalcitePercent[j]==0 && QuartzPercent[j]==0 && TPorosity.BulkDensity[j] < (-0.0173 * TPorosity.Neutron[j]) + 2.9162)
{
DolomitePercent[j] = 100;
}
if (DolomitePercent[j] == 0 && CalcitePercent[j] == 0 && QuartzPercent[j] == 0 && TPorosity.BulkDensity[j]> 2.5943 * Math.Pow(Math.E, (-0.009 * TPorosity.Neutron[j])))
{
QuartzPercent[j] = 100;
}
}
return Tuple.Create(DolomitePercent, CalcitePercent, QuartzPercent);
}
}
希望有人帮助我。 谢谢