//开始计算 private void btnCalculate_Click(object sender, EventArgs e) { btnCalculate.Enabled = false; if (string.IsNullOrEmpty(textBox1.Text)) { MsgBox.ShowInfo("请输入站号信息"); return; } if (!backgroundWorker1.IsBusy) { CaculateInfo info = new CaculateInfo() { a0 = double.Parse(numericUpDown1.Value.ToString()), N = int.Parse(numericUpDown2.Value.ToString()), CaculateDate = dateTimePicker1.Value, StateNo = textBox1.Text }; Label label6 = new Label(); label6.Height = 60; label6.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; label6.Dock = System.Windows.Forms.DockStyle.Top; label6.Font = new System.Drawing.Font("楷体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); label6.BackColor = Color.White; label6.Location = new System.Drawing.Point(0, 0); label6.TabIndex = 0; label6.Text = $"({DateTime.Now}):"; label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; this.panel2.Controls.Add(label6); info.lbl = label6; backgroundWorker1.RunWorkerAsync(info); } }
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { CaculateInfo info = e.UserState as CaculateInfo; if (e.ProgressPercentage < 100) { info.lbl.Text = info.lbl.Text + $"{info.StateNo}+"; } else { info.lbl.Text = info.lbl.Text + $"{info.StateNo} = {info.result}"; } }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { CaculateInfo info = e.Argument as CaculateInfo; double result = 0; for (int i = 0; i < info.N; i++) { double rain = 0; //计算雨量值和系数值 DataSet ds = SqlHelper.ExecuteDataset(SqlHelper.GetConnection(), CommandType.Text, $"SELECT RAINFALL_1_DAY FROM RAINFALL_DAY where time = '{info.CaculateDate.AddDays(i * (-1)).ToString("yyyy-MM-dd")}' and monitornum = '{info.StateNo}'"); if (ds.Tables[0].Rows.Count != 0) { rain = double.Parse(ds.Tables[0].Rows[0][0].ToString()); } //系数 double num = Math.Round(Math.Pow(info.a0, i), 2); result += num * rain; CaculateInfo temp = null; if (i != info.N - 1) { temp = new CaculateInfo() { StateNo = $"{ num}*{rain}", lbl = info.lbl } } ; else { temp = new CaculateInfo() { StateNo = $"{ num}*{rain}", lbl = info.lbl, result = result.ToString() } }; backgroundWorker1.ReportProgress((i + 1) * 100 / info.N, temp); } }