关闭程序后,显示程序运行时间。代码如下:

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Diagnostics; 10 namespace time 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 private Stopwatch stop= new Stopwatch(); 19 private void Form1_Load(object sender, EventArgs e) 20 { 21 stop.Start(); 22 } 23 24 private void Form1_FormClosing(object sender, FormClosingEventArgs e) 25 { 26 DialogResult dr = MessageBox.Show("真的要退出?", "退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question); 27 if (dr == DialogResult.Yes) 28 { 29 stop.Stop(); 30 MessageBox.Show("程序共运行时间:" + stop.Elapsed.Seconds.ToString() + "秒"); 31 e.Cancel = false; 32 } 33 else 34 { 35 e.Cancel = true; 36 } 37 } 38 } 39 }