[corlib] Handle overflow correctly in Timer to avoid spinning. Fixes #8782
[mono.git] / mcs / class / corlib / System.Threading / Timer.cs
index 7bebd9c013bd1554fd4285ed54e5a61454841f0c..0f084b5fe39b832800f0395ae797878e161fe070 100644 (file)
@@ -381,10 +381,14 @@ namespace System.Threading
                                                //PrintList ();
                                                ms_wait = -1;
                                                if (min_next_run != Int64.MaxValue) {
-                                                       long diff = min_next_run - DateTime.GetTimeMonotonic (); 
-                                                       ms_wait = (int)(diff / TimeSpan.TicksPerMillisecond);
-                                                       if (ms_wait < 0)
-                                                               ms_wait = 0;
+                                                       long diff = (min_next_run - DateTime.GetTimeMonotonic ())  / TimeSpan.TicksPerMillisecond;
+                                                       if (diff > Int32.MaxValue)
+                                                               ms_wait = Int32.MaxValue - 1;
+                                                       else {
+                                                               ms_wait = (int)(diff);
+                                                               if (ms_wait < 0)
+                                                                       ms_wait = 0;
+                                                       }
                                                }
                                        }
                                        // Wait until due time or a timer is changed and moves from/to the first place in the list.