日期:2014-05-18 浏览次数:22482 次
System.DateTime oldtime=new DateTime(); delegate void SetTextCallback(string strTime);//定义托管函数 System.Timers.Timer t = new System.Timers.Timer(100);//实例化Timer类 bool closeFlag; public Form1() { InitializeComponent(); } private void timeClass_Click(object sender, EventArgs e) { t.Elapsed+= new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件; t.AutoReset = true;//设置是执行一次(false)还是一直执行(true); t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件; } public void theout(object source, System.Timers.ElapsedEventArgs e) //定时器调用 { System.DateTime currentTime = new DateTime(); currentTime = DateTime.Now; string ss = (currentTime - oldtime).TotalMilliseconds.ToString(); oldtime = currentTime; AddText(ss); } void AddText(string strTime) { //在此处使用过:this.isDisposed和this.Disposing进行判定,无法解决 if (this.listView1.InvokeRequired) { SetTextCallback d = new SetTextCallback(AddText); this.Invoke(d, new object[] { strTime });//此处出现,无法释放问题 } else { string[] str = { listView1.Items.Count.ToString(), "测试", strTime }; ListViewItem item = new ListViewItem(str); listView1.Items.Add(item); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { t.Close();//这个地方如果增加一个关闭t.close的话也不行,定时器整个form关闭前没有被关闭! Thread.Sleep(500); }