[OpenTK] Do not hang when update rate too high
authorStefanos A. <stapostol@gmail.com>
Tue, 14 Jan 2014 12:55:24 +0000 (13:55 +0100)
committerStefanos A. <stapostol@gmail.com>
Tue, 14 Jan 2014 12:55:24 +0000 (13:55 +0100)
OpenTK will now detect when an UpdateFrame handler is consistently
taking too long to finish, and stop raising UpdateFrame events. This
gives ProcessEvents() a chance to execute and will protect the
application from hanging up.

Source/OpenTK/GameWindow.cs

index 9200216..5cd8b81 100644 (file)
@@ -92,6 +92,8 @@ namespace OpenTK
 
         double update_epsilon; // quantization error for UpdateFrame events
 
+        bool is_running_slowly; // true, when UpdatePeriod cannot reach TargetUpdatePeriod
+
         VSyncMode vsync;
 
         FrameEventArgs update_args = new FrameEventArgs();
@@ -440,6 +442,7 @@ namespace OpenTK
 
         void DispatchUpdateAndRenderFrame(object sender, EventArgs e)
         {
+            int is_running_slowly_retries = 4;
             double timestamp = watch.Elapsed.TotalSeconds;
             double elapsed = 0;
 
@@ -464,6 +467,14 @@ namespace OpenTK
                     // per ProcessEvents() call)
                     break;
                 }
+
+                is_running_slowly = update_epsilon >= TargetUpdatePeriod;
+                if (is_running_slowly && --is_running_slowly_retries == 0)
+                {
+                    // If UpdateFrame consistently takes longer than TargetUpdateFrame
+                    // stop raising events to avoid hanging inside the UpdateFrame loop.
+                    break;
+                }
             }
 
             elapsed = ClampElapsed(timestamp - render_timestamp);