0
在这里,我在C#中使用简单的逻辑。我从两个DateTimePicker
s中减去天数,并生成与总天数相同的GridView
行。我把这个逻辑放在一个Timer
中,它的间隔为100毫秒,并自动执行。这适用于90-100行,但如果它变为1000行,那么程序会卡住。所以,我想我需要使用线程。我怎样才能做到这一点?我知道基本的多线程,并试图这样,没有工作:多线程的GridView更新
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();
}
你想做什么?这个故事可以帮助我们理解情节。无论哪种方式,你应该阅读关于BackgroundWorkers [这里](http://www.dotnetperls.com/backgroundworker)和[这里](http://msdn.microsoft.com/en-us/library/cc221403(v = VS.95)的.aspx)。 – SimpleVar
好的,我想创建新的线程所以,即使有10K行,它也要干扰主程序,并在场景后面运行。 –
@YoryeNathan:我试图使用Bcakgroundworkers,但得到相同的错误:“InvalidOperationException - 跨线程操作无效:控制'ed_Picker'从其创建的线程以外的线程访问。 myMethod方法的第一行出现此错误。 –