在这里,我在 C# 中使用简单的逻辑。我从两个DateTimePicker
s 中减去天数并生成与GridView
总天数相同的行数。我把这个逻辑放在 aTimer
中,它以 100 毫秒的间隔滴答并自动执行。这适用于 90-100 行,但如果它达到 1000 行,那么程序就会卡住。所以,我想我需要使用线程。我怎样才能做到这一点?我知道基本的多线程,并尝试了这个,但没有奏效:
private void timer1_Tick(object sender, EventArgs e)
{
myMethod();
}
void myMethod() {
ed_Picker.MinDate = sd_Picker.Value;
TimeSpan ts = new TimeSpan();
//GridView.Rows.cont
label3.Text = ed_Picker.Value.CompareTo(sd_Picker.Value).ToString();
//dateTimePicker2.MinDate = dateTimePicker1.Value;
ts = ed_Picker.Value.Subtract(sd_Picker.Value);
double d;
d = Math.Ceiling(ts.TotalDays) + 1;
label3.Text = d.ToString();
DataGridViewRow row = new DataGridViewRow();
GridView.RowCount = Int32.Parse(d.ToString()); // This is key line which I like to put in new Thread
for (int i = 1; i <= GridView.RowCount; i++)
{
GridView.Rows[i - 1].Cells[0].Value = sd_Picker.Value.AddDays(Convert.ToDouble(i - 1)).ToLongDateString();
GridView.Rows[i - 1].Cells[0].ReadOnly = true;
}
DateTimeConverter dtc = new DateTimeConverter();
TimeSpan ts1 = new TimeSpan();
try
{
for (int i = 0; i < GridView.RowCount; i++)
{
ts1 += Convert.ToDateTime(GridView.Rows[i].Cells[2].Value).Subtract(Convert.ToDateTime(GridView.Rows[i].Cells[1].Value));
}
}
catch (Exception ex)
{
}
label6.Text = ts1.ToString();
}