From 1f44cf27a14a77c544f0f8bef96cf8c62e752a30 Mon Sep 17 00:00:00 2001 From: "Stefanos A." Date: Tue, 14 Jan 2014 13:55:24 +0100 Subject: [PATCH] [OpenTK] Do not hang when update rate too high 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 | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Source/OpenTK/GameWindow.cs b/Source/OpenTK/GameWindow.cs index 9200216..5cd8b81 100644 --- a/Source/OpenTK/GameWindow.cs +++ b/Source/OpenTK/GameWindow.cs @@ -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); -- 2.7.4