初始化开始倒计时,双击某行,某行重新倒计时,时间到时, 执行某个操作。求源码,求改下源码,谢谢
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
DateTime dtStart;
List<DateTime> str = new List<DateTime>();
private void btnStart_Click(object sender, EventArgs e)
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
dtStart = DateTime.Now.AddMinutes(0.5);
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
return;
}
private void frmMain_Load(object sender, EventArgs e)
{
//使用List<>泛型集合填充DataGridView
List<Student> students = new List<Student>();
Student hat = new Student("Hathaway", "12", "Male","");
Student peter = new Student("Peter", "14", "Male", "");
//Student dell = new Student("Dell", "16", "Male", "");
//Student anne = new Student("Anne", "19", "Female", "");
System.Threading.Timer timerClose;
students.Add(hat);
students.Add(peter);
//students.Add(dell);
//students.Add(anne);
this.dataGridView1.DataSource = students;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (i == 0)
{
DateTime dtStart1 = DateTime.Now.AddMinutes(0.5);
str.Add(dtStart1);
}
else {
DateTime dtStart2 = DateTime.Now.AddMinutes(0.5);
str.Add(dtStart2);
}
timerClose = new System.Threading.Timer(new TimerCallback(timerCall), i, 1000, 1000);
}
}
private void timerCall(object obj)
{
int res = Convert.ToInt32(obj);
TimeSpan ts = str[res] - DateTime.Now;
this.dataGridView1.Rows[res].Cells[3].Value = string.Format("{0}分钟{1}秒", ts.Minutes, ts.Seconds);
if (ts < TimeSpan.Zero)
{
this.dataGridView1.Rows[res].Cells[3].Value = "超时";
}
}
private void CellDoubltimer(object obj)
{
DateTime Start = DateTime.Now.AddMinutes(0.5);//最后设置25
int res = Convert.ToInt32(obj);
TimeSpan ts = Start - DateTime.Now;
this.dataGridView1.Rows[res].Cells[3].Value = string.Format("{0}分钟{1}秒", ts.Minutes, ts.Seconds);
if (ts < TimeSpan.Zero)
{
this.dataGridView1.Rows[res].Cells[3].Value = "超时";
}
}
public class Student {
public string Name { get; set; }
public string age { get; set; }
public string xinbie { get; set; }
public string times { get; set; }
public Student(string v1, string v2, string v3,string v4)
{
this.Name = v1;
this.age = v2;
this.xinbie = v3;
this.times = v4;
}
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
System.Threading.Timer timerClose;
timerClose = new System.Threading.Timer(new TimerCallback(CellDoubltimer), e.RowIndex, 0, 1000);
}
}