Advertisement
sebbu

MyTimer example

Oct 9th, 2019
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7.  
  8. namespace MSIExport
  9. {
  10.     class MyTimer : IDisposable
  11.     {
  12.         Stopwatch timer;
  13.         Action<TimeSpan> value;
  14.         public MyTimer(Action<TimeSpan> val)
  15.         {
  16.             value = val;
  17.             timer = new Stopwatch();
  18.             timer.Start();
  19.         }
  20.         public void Dispose()
  21.         {
  22.             timer.Stop();
  23.             value(timer.Elapsed);
  24.         }
  25.     }
  26.     public static void main(string[] args)
  27.     {
  28.         TimeSpan duration = new TimeSpan();
  29.         using (MyTimer mt = new MyTimer(x => duration = x))
  30.         {
  31.             Thread.Sleep(5000);
  32.         }
  33.         Console.WriteLine("duration : {0}", duration);
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement