在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
using System.Diagnostics; private Stopwatch stw = new Stopwatch(); private void Form1_Load(object sender, EventArgs e) { stw.Start(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { DialogResult dr = MessageBox.Show("真的要退出?", "退出", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr == DialogResult.Yes) { stw.Stop(); MessageBox.Show("程序共运行时间:" + stw.Elapsed.Seconds.ToString() + "秒"); e.Cancel = false; } else { e.Cancel = true; } } 在.net1.1中,需要自己写 using System; namespace StopWatchTest { class Class1 { [STAThread] static void Main(string[] args) { StopWatch sw = new StopWatch(); sw.start(); for (long i = 0 ; i < 100000000 ; i++) { } Console.WriteLine(sw.elapsed()); Console.Read(); } } class StopWatch { private int mintStart; public void start() { mintStart = Environment.TickCount; } public long elapsed() { return Environment.TickCount - mintStart; } } } |
请发表评论