借助于using语法,像下面的代码所示,可以检查一段代码的运行时间,并打印在控制台上。
- using (new AutoStopwatch()) {
- Decimal total2 = 0; int limit2 = 1000000;
- for (int i = 0; i < limit2; ++i) {
- total2 = total2 + (Decimal)Math.Sqrt(i); }
- }
10 使用光标
当程序正在后台运行保存或是册除操作时,应当将光标状态修改为忙碌。可使用下面的技巧。
- class AutoWaitCursor : IDisposable {
- private Control _target; private Cursor _prevCursor = Cursors.Default;
- public AutoWaitCursor(Control control) {
- if (control == null) {
- throw new ArgumentNullException(“control”); }
- _target = control; _prevCursor = _target.Cursor;
- _target.Cursor = Cursors.WaitCursor; }
- public void Dispose() {
- _target.Cursor = _prevCursor; }
- }










