我有这个 ListView。我的目标是在列表中的所有时间/选定时间中添加/减去固定时间。问题是其中会有大约 1000-3000 行,所以需要一些时间。我的目标是让计算机尽可能快地做到这一点。
现在,如果我以垃圾邮件方式运行该函数,则表单需要一些时间才能完成,如果我选择对选定的行执行此操作,其中一半,则表单会在完成前冻结一段时间。
我的代码现在看起来像这样(这是代码的图片以便更好地查看):
public void PlusMinus(bool plus)
{
int items_to_set = 0;
int msec_start = 0;
int msec_end = 0;
int msec_box = Convert.ToInt32(mskTime.Text.Substring(9, 3)) +
(Convert.ToInt32(mskTime.Text.Substring(6, 2)) * 1000) +
(Convert.ToInt32(mskTime.Text.Substring(3, 2)) * 60000) +
(Convert.ToInt32(mskTime.Text.Substring(0, 2)) * 3600000);
if (rbtnAll.Checked) { items_to_set = listSub.Items.Count; }
else { items_to_set = listSub.SelectedItems.Count; }
if (rbtnAll.Checked)
{
for (int i = 0; i < items_to_set; i++)
{
msec_start = Convert.ToInt32(listSub.Items[i].SubItems[1].Text.Substring(9, 3)) +
(Convert.ToInt32(listSub.Items[i].SubItems[1].Text.Substring(6, 2)) * 1000) +
(Convert.ToInt32(listSub.Items[i].SubItems[1].Text.Substring(3, 2)) * 60000) +
(Convert.ToInt32(listSub.Items[i].SubItems[1].Text.Substring(0, 2)) * 3600000);
msec_end = Convert.ToInt32(listSub.Items[i].SubItems[2].Text.Substring(9, 3)) +
(Convert.ToInt32(listSub.Items[i].SubItems[2].Text.Substring(6, 2)) * 1000) +
(Convert.ToInt32(listSub.Items[i].SubItems[2].Text.Substring(3, 2)) * 60000) +
(Convert.ToInt32(listSub.Items[i].SubItems[2].Text.Substring(0, 2)) * 3600000);
if (plus == true) { msec_start += msec_box; msec_end += msec_box; }
else { msec_start -= msec_box; msec_end -= msec_box; }
TimeSpan ts = TimeSpan.FromMilliseconds(msec_start);
listSub.Items[i].SubItems[1].Text = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D3}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);
ts = TimeSpan.FromMilliseconds(msec_end);
listSub.Items[i].SubItems[2].Text = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D3}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);
}
}
else
{
for (int i = 0; i < items_to_set; i++)
{
msec_start = Convert.ToInt32(listSub.Items[listSub.SelectedIndices[i]].SubItems[1].Text.Substring(9, 3)) +
(Convert.ToInt32(listSub.Items[listSub.SelectedIndices[i]].SubItems[1].Text.Substring(6, 2)) * 1000) +
(Convert.ToInt32(listSub.Items[listSub.SelectedIndices[i]].SubItems[1].Text.Substring(3, 2)) * 60000) +
(Convert.ToInt32(listSub.Items[listSub.SelectedIndices[i]].SubItems[1].Text.Substring(0, 2)) * 3600000);
msec_end = Convert.ToInt32(listSub.Items[listSub.SelectedIndices[i]].SubItems[2].Text.Substring(9, 3)) +
(Convert.ToInt32(listSub.Items[listSub.SelectedIndices[i]].SubItems[2].Text.Substring(6, 2)) * 1000) +
(Convert.ToInt32(listSub.Items[listSub.SelectedIndices[i]].SubItems[2].Text.Substring(3, 2)) * 60000) +
(Convert.ToInt32(listSub.Items[listSub.SelectedIndices[i]].SubItems[2].Text.Substring(0, 2)) * 3600000);
if (plus == true) { msec_start += msec_box; msec_end += msec_box; }
else { msec_start -= msec_box; msec_end -= msec_box; }
TimeSpan ts = TimeSpan.FromMilliseconds(msec_start);
listSub.Items[listSub.SelectedIndices[i]].SubItems[1].Text = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D3}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);
ts = TimeSpan.FromMilliseconds(msec_end);
listSub.Items[listSub.SelectedIndices[i]].SubItems[2].Text = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D3}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);
}
}
}
那么我是否可以做出一些改变,这会对这里的性能产生很大的积极影响?