[System] Removal of the NET_2_0 in the source code
[mono.git] / mcs / class / System / System.Diagnostics / Stopwatch.cs
index 0f70c917f6d454b3b339f194d6c4cfc72f838765..e0efe160429494da26552fa289a889249eb15cd7 100644 (file)
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
-
 using System;
 using System.ComponentModel;
+using System.Runtime.InteropServices;
+using System.Runtime.CompilerServices;
 
 namespace System.Diagnostics
 {
        public class Stopwatch
        {
-               [MonoTODO ("high resolution mode support")]
-               public static readonly long Frequency;
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               public static extern long GetTimestamp ();
 
-               [MonoTODO ("high resolution mode support")]
-               public static readonly bool IsHighResolution;
+               public static readonly long Frequency = 10000000;
 
-               [MonoTODO ("high resolution mode support")]
-               public static long GetTimestamp ()
-               {
-                       return DateTime.Now.Ticks;
-               }
+               public static readonly bool IsHighResolution = true;
 
                public static Stopwatch StartNew ()
                {
@@ -57,12 +52,6 @@ namespace System.Diagnostics
                        return s;
                }
 
-               static Stopwatch ()
-               {
-                       Frequency = TimeSpan.TicksPerSecond;
-                       IsHighResolution = false;
-               }
-
                public Stopwatch ()
                {
                }
@@ -71,14 +60,30 @@ namespace System.Diagnostics
                long started;
                bool is_running;
 
-               [MonoTODO ("high resolution mode support")]
                public TimeSpan Elapsed {
-                       get { return TimeSpan.FromTicks (ElapsedTicks); }
+                       get {
+                               if (IsHighResolution) {
+                                       // convert our ticks to TimeSpace ticks, 100 nano second units
+                                       // using two divisions helps avoid overflow
+                                       return TimeSpan.FromTicks ((long)(ElapsedTicks / (Frequency / TimeSpan.TicksPerSecond)));
+                               }
+                               else {
+                                       return TimeSpan.FromTicks (ElapsedTicks); 
+                               }
+                       }
                }
 
-               [MonoTODO ("high resolution mode support")]
                public long ElapsedMilliseconds {
-                       get { checked { return (long) Elapsed.TotalMilliseconds; } }
+                       get { 
+                               checked {
+                                       if (IsHighResolution) {
+                                               return (long)(ElapsedTicks / (Frequency / 1000));
+                                       }
+                                       else {
+                                               return (long) Elapsed.TotalMilliseconds;
+                                       }
+                               } 
+                       }
                }
 
                public long ElapsedTicks {
@@ -110,7 +115,15 @@ namespace System.Diagnostics
                        elapsed += GetTimestamp () - started;
                        is_running = false;
                }
+
+#if NET_4_0 || MOBILE
+               public void Restart ()
+               {
+                       started = GetTimestamp ();
+                       elapsed = 0;
+                       is_running = true;
+               }
+#endif
        }
 }
 
-#endif