From 205820733b5ec057e53eb84b9b4f80b39b0f1a9a Mon Sep 17 00:00:00 2001 From: in0finite Date: Mon, 24 Jan 2022 18:08:08 +0100 Subject: [PATCH] add multiple iterations when waiting for jobs to finish --- Assets/Scripts/Editor/AssetExporter.cs | 47 +++++++++++++++++--------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/Assets/Scripts/Editor/AssetExporter.cs b/Assets/Scripts/Editor/AssetExporter.cs index 1080bbc0..a018d716 100644 --- a/Assets/Scripts/Editor/AssetExporter.cs +++ b/Assets/Scripts/Editor/AssetExporter.cs @@ -297,6 +297,7 @@ namespace SanAndreasUnity.Editor $"\r\nobjects processed {i}/{objectsToExport.Length}", i / (float)objectsToExport.Length, nextNextIndex / (float)objectsToExport.Length, + 4, isCanceledRef)) yield return item; @@ -347,35 +348,49 @@ namespace SanAndreasUnity.Editor EditorUtility.DisplayDialog("", $"Finished ! \r\n\r\n{displayText}", "Ok"); } - private static IEnumerable WaitForCompletionOfLoadingJobs(string textSuffix, float startPerc, float endPerc, Ref isCanceledRef) + private static IEnumerable WaitForCompletionOfLoadingJobs( + string textSuffix, + float startPerc, + float endPerc, + int numIterations, + Ref isCanceledRef) { + if (numIterations < 1) + throw new ArgumentOutOfRangeException(nameof(numIterations)); + isCanceledRef.value = false; float diffPerc = endPerc - startPerc; - long initialNumPendingJobs = LoadingThread.Singleton.GetNumPendingJobs(); - // TODO: this should be removed yield return null; // this must be done, otherwise LoadingThread does not start processing any job - long numPendingJobs; - do + for (int i = 0; i < numIterations; i++) { - LoadingThread.Singleton.UpdateJobs(); + long initialNumPendingJobs = LoadingThread.Singleton.GetNumPendingJobs(); + long numPendingJobs = initialNumPendingJobs; - numPendingJobs = LoadingThread.Singleton.GetNumPendingJobs(); - long numJobsProcessed = initialNumPendingJobs - numPendingJobs; - - float currentPerc = startPerc + diffPerc * (numJobsProcessed / (float)initialNumPendingJobs); - if (EditorUtility.DisplayCancelableProgressBar("", $"Waiting for async jobs to finish ({numJobsProcessed}/{initialNumPendingJobs})...{textSuffix}", currentPerc)) + do { - isCanceledRef.value = true; - yield break; - } + long numJobsProcessed = initialNumPendingJobs - numPendingJobs; - System.Threading.Thread.Sleep(10); // don't interact with background thread too often, and also reduce CPU usage + float currentPerc = startPerc + diffPerc * (0 == initialNumPendingJobs ? 0f : numJobsProcessed / (float)initialNumPendingJobs); + if (EditorUtility.DisplayCancelableProgressBar("", $"Waiting for async jobs to finish ({numJobsProcessed}/{initialNumPendingJobs})...{textSuffix}", currentPerc)) + { + isCanceledRef.value = true; + yield break; + } - } while (numPendingJobs > 0); + LoadingThread.Singleton.UpdateJobs(); + + //System.Threading.Thread.Sleep(10); // don't interact with background thread too often, and also reduce CPU usage + yield return null; + + numPendingJobs = LoadingThread.Singleton.GetNumPendingJobs(); + initialNumPendingJobs = Math.Max(initialNumPendingJobs, numPendingJobs); + + } while (numPendingJobs > 0); + } }