diff --git a/BrawlLib/SSBB/ResourceNodes/Modules/RELNode.cs b/BrawlLib/SSBB/ResourceNodes/Modules/RELNode.cs index 5cc72a2f2..04bf10149 100644 --- a/BrawlLib/SSBB/ResourceNodes/Modules/RELNode.cs +++ b/BrawlLib/SSBB/ResourceNodes/Modules/RELNode.cs @@ -8,6 +8,7 @@ using System.Diagnostics; using System.IO; using System.Text; +using System.Threading; using System.Windows.Forms; namespace BrawlLib.SSBB.ResourceNodes @@ -297,6 +298,27 @@ public byte? ItemID4 public override void Dispose() { + if (!Populated) + { + int timeoutSecs = 5; + // wait 5 seconds to see if population finishes + while (!Populated && timeoutSecs >= 0) + { + Thread.Sleep(1000); + --timeoutSecs; + } + // attempt to cancel population if it hasn't finished still + if (!Populated) + { + populator.CancelAsync(); + for (int i = 0; i < 5; i++) // wait 5 more seconds for cancellation to finish hopefully + { + Thread.Sleep(1000); + } + Debug.WriteLine("Cancelled population"); + } + } + populator?.Dispose(); _files.Remove(ModuleID); base.Dispose(); } @@ -445,18 +467,21 @@ public override void OnPopulate() } } } - Sections[5].Populate(); watch.Stop(); Console.WriteLine("Took {0} seconds to relocate {1} module", watch.ElapsedMilliseconds / 1000d, Name); }; - using (BackgroundWorker b = new BackgroundWorker()) + Populated = false; + populator = new BackgroundWorker(); + populator.WorkerSupportsCancellation = true; + populator.RunWorkerCompleted += (sender, args) => { - b.DoWork += new DoWorkEventHandler(work); - b.RunWorkerAsync(); - } + Populated = true; + }; + populator.DoWork += new DoWorkEventHandler(work); + populator.RunWorkerAsync(); // Stage module conversion byte* bptr = (byte*) WorkingUncompressed.Address; @@ -493,6 +518,9 @@ public override void OnPopulate() UpdateItemIDs(); } + private BackgroundWorker populator; + public bool Populated = false; + public void ApplyRelocations() { foreach (ModuleSectionNode r in Sections)