From 4415300a1f76e269ec0611d2850d7b2b5e9f21f4 Mon Sep 17 00:00:00 2001 From: John Lewin Date: Wed, 20 Dec 2017 18:25:12 -0800 Subject: [PATCH] Move heavy tasks out of constructor, make awaitable - Add support for awaitable startup - Issue MatterHackers/MCCentral#2423 Select printer not changing to new printer --- ApplicationView/ApplicationController.cs | 12 +-- ApplicationView/PrinterModels.cs | 33 ++++---- Program.cs | 9 +-- .../Settings/ProfileManager.cs | 77 +++++++++---------- .../MatterControl/SliceSettingsFieldTests.cs | 2 +- 5 files changed, 66 insertions(+), 67 deletions(-) diff --git a/ApplicationView/ApplicationController.cs b/ApplicationView/ApplicationController.cs index 789e85d31..142c043a8 100644 --- a/ApplicationView/ApplicationController.cs +++ b/ApplicationView/ApplicationController.cs @@ -93,7 +93,7 @@ namespace MatterHackers.MatterControl // A list of printers which are open (i.e. displaying a tab) on this instance of MatterControl public IEnumerable ActivePrinters { get; } = new List(); - private static PrinterConfig emptyPrinter = new PrinterConfig(null, PrinterSettings.Empty); + private static PrinterConfig emptyPrinter = new PrinterConfig(PrinterSettings.Empty); private static string cacheDirectory = Path.Combine(ApplicationDataStorage.ApplicationUserDataPath, "data", "temp", "cache"); @@ -1508,7 +1508,7 @@ namespace MatterHackers.MatterControl private static Stopwatch timer; public static string PlatformFeaturesProvider { get; set; } = "MatterHackers.MatterControl.WindowsPlatformsFeatures, MatterControl"; - + public static SystemWindow LoadRootWindow(int width, int height) { timer = Stopwatch.StartNew(); @@ -1554,13 +1554,13 @@ namespace MatterHackers.MatterControl ReportStartupProgress(0.02, "First draw->RunOnIdle"); //UiThread.RunOnIdle(() => - Task.Run(() => + Task.Run(async () => { ReportStartupProgress(0.1, "Datastore"); Datastore.Instance.Initialize(); ReportStartupProgress(0.15, "MatterControlApplication.Initialize"); - var mainView = Initialize(systemWindow, (progress0To1, status) => + var mainView = await Initialize(systemWindow, (progress0To1, status) => { ReportStartupProgress(0.2 + progress0To1 * 0.7, status); }); @@ -1583,7 +1583,7 @@ namespace MatterHackers.MatterControl return systemWindow; } - public static GuiWidget Initialize(SystemWindow systemWindow, Action reporter) + public static async Task Initialize(SystemWindow systemWindow, Action reporter) { AppContext.Platform = AggContext.CreateInstanceFrom(PlatformFeaturesProvider); @@ -1608,6 +1608,8 @@ namespace MatterHackers.MatterControl reporter?.Invoke(0.2, "ProfileManager"); bool na2 = ProfileManager.Instance.IsGuestProfile; + await ProfileManager.Instance.Initialize(); + reporter?.Invoke(0.3, "MainView"); ApplicationController.Instance.MainView = new WidescreenPanel(); diff --git a/ApplicationView/PrinterModels.cs b/ApplicationView/PrinterModels.cs index a3d57f05d..195d15724 100644 --- a/ApplicationView/PrinterModels.cs +++ b/ApplicationView/PrinterModels.cs @@ -582,6 +582,19 @@ namespace MatterHackers.MatterControl public class PrinterConfig { public BedConfig Bed { get; } + + private EventHandler unregisterEvents; + + public PrinterConfig(PrinterSettings settings) + { + this.Bed = new BedConfig(this); + this.Connection = new PrinterConnection(printer: this); + this.Settings = settings; + this.Settings.printer = this; + + ActiveSliceSettings.SettingChanged.RegisterEvent(Printer_SettingChanged, ref unregisterEvents); + } + public PrinterViewState ViewState { get; } = new PrinterViewState(); private PrinterSettings _settings; @@ -601,23 +614,17 @@ namespace MatterHackers.MatterControl public PrinterConnection Connection { get; private set; } - private EventHandler unregisterEvents; - - public PrinterConfig(EditContext editContext, PrinterSettings settings) + /// + /// Loads content to the bed and prepares the printer for use + /// + /// + /// + public async Task Initialize(EditContext editContext) { - this.Bed = new BedConfig(this); - if (editContext != null) { - this.Bed.LoadContent(editContext).ConfigureAwait(false); + await this.Bed.LoadContent(editContext); } - - this.Connection = new PrinterConnection(printer: this); - - this.Settings = settings; - this.Settings.printer = this; - - ActiveSliceSettings.SettingChanged.RegisterEvent(Printer_SettingChanged, ref unregisterEvents); } internal void SwapToSettings(PrinterSettings printerSettings) diff --git a/Program.cs b/Program.cs index f9efe3c84..7c30034cc 100644 --- a/Program.cs +++ b/Program.cs @@ -1,14 +1,8 @@ using System; -using System.Diagnostics; using System.Globalization; using System.IO; using System.Threading; -using System.Threading.Tasks; -using MatterHackers.Agg; -using MatterHackers.Agg.Font; using MatterHackers.Agg.Platform; -using MatterHackers.Agg.UI; -using MatterHackers.MatterControl.DataStorage; using MatterHackers.MatterControl.SettingsManagement; using Mindscape.Raygun4Net; @@ -52,7 +46,8 @@ namespace MatterHackers.MatterControl //var systemWindow = new DesktopMainWindow(400, 200) var (width, height) = RootSystemWindow.GetStartupBounds(); - Application.LoadRootWindow(width, height).ShowAsSystemWindow(); + var systemWindow = Application.LoadRootWindow(width, height); + systemWindow.ShowAsSystemWindow(); } private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) diff --git a/SlicerConfiguration/Settings/ProfileManager.cs b/SlicerConfiguration/Settings/ProfileManager.cs index 8feefd0ac..5c27ce59a 100644 --- a/SlicerConfiguration/Settings/ProfileManager.cs +++ b/SlicerConfiguration/Settings/ProfileManager.cs @@ -45,32 +45,7 @@ namespace MatterHackers.MatterControl.SlicerConfiguration { public static RootedObjectEventHandler ProfilesListChanged = new RootedObjectEventHandler(); - private static ProfileManager activeInstance = null; - public static ProfileManager Instance - { - get => activeInstance; - private set - { - activeInstance = value; - - // Select a 'LastProfile' exists and it is missing from ActivePrinters, load it - var lastProfile = activeInstance[activeInstance.LastProfileID]; - if (lastProfile != null - && !ApplicationController.Instance.ActivePrinters.Where(p => p.Settings.ID == lastProfile.ID).Any()) - { - // Load or download on a background thread the last loaded settings - ApplicationController.Instance.SetActivePrinter( - new PrinterConfig( - new EditContext() - { - ContentStore = ApplicationController.Instance.Library.PlatingHistory, - SourceItem = BedConfig.GetLastPlateOrNew() - }, - // Short term workaround to run sync during load - LoadProfileAsync(activeInstance.LastProfileID).Result)).Wait(); - } - } - } + public static ProfileManager Instance { get; private set; } private static EventHandler unregisterEvents; @@ -86,6 +61,26 @@ namespace MatterHackers.MatterControl.SlicerConfiguration ReloadActiveUser(); } + public async Task Initialize() + { + // Select a 'LastProfile' exists and it is missing from ActivePrinters, load it + var lastProfile = this[this.LastProfileID]; + if (lastProfile != null + && !ApplicationController.Instance.ActivePrinters.Where(p => p.Settings.ID == lastProfile.ID).Any()) + { + // TODO: This application init code should move to caller and be awaited + var printer = new PrinterConfig(await LoadProfileAsync(this.LastProfileID)); + + await printer.Initialize(new EditContext() + { + ContentStore = ApplicationController.Instance.Library.PlatingHistory, + SourceItem = BedConfig.GetLastPlateOrNew() + }); + + await ApplicationController.Instance.SetActivePrinter(printer); + } + } + public string UserName { get; set; } /// @@ -147,14 +142,15 @@ namespace MatterHackers.MatterControl.SlicerConfiguration { ProfileManager.Instance.LastProfileID = printerID; - await ApplicationController.Instance.SetActivePrinter( - new PrinterConfig( - new EditContext() - { - ContentStore = ApplicationController.Instance.Library.PlatingHistory, - SourceItem = BedConfig.GetLastPlateOrNew() - }, - await ProfileManager.LoadProfileAsync(printerID))); + var printer = new PrinterConfig(await ProfileManager.LoadProfileAsync(printerID)); + + await printer.Initialize(new EditContext() + { + ContentStore = ApplicationController.Instance.Library.PlatingHistory, + SourceItem = BedConfig.GetLastPlateOrNew() + }); + + await ApplicationController.Instance.SetActivePrinter(printer); } /// @@ -481,13 +477,12 @@ namespace MatterHackers.MatterControl.SlicerConfiguration // Set as active profile ProfileManager.Instance.LastProfileID = guid; - var printer = new PrinterConfig( - new EditContext() - { - ContentStore = ApplicationController.Instance.Library.PlatingHistory, - SourceItem = BedConfig.GetLastPlateOrNew() - }, - printerSettings); + var printer = new PrinterConfig(printerSettings); + await printer.Initialize(new EditContext() + { + ContentStore = ApplicationController.Instance.Library.PlatingHistory, + SourceItem = BedConfig.GetLastPlateOrNew() + }); await ApplicationController.Instance.SetActivePrinter(printer); diff --git a/Tests/MatterControl.Tests/MatterControl/SliceSettingsFieldTests.cs b/Tests/MatterControl.Tests/MatterControl/SliceSettingsFieldTests.cs index 5c00b24ca..8f199222a 100644 --- a/Tests/MatterControl.Tests/MatterControl/SliceSettingsFieldTests.cs +++ b/Tests/MatterControl.Tests/MatterControl/SliceSettingsFieldTests.cs @@ -277,7 +277,7 @@ namespace MatterControl.Tests.MatterControl AggContext.StaticData = new FileSystemStaticData(TestContext.CurrentContext.ResolveProjectPath(4, "StaticData")); MatterControlUtilities.OverrideAppDataLocation(TestContext.CurrentContext.ResolveProjectPath(4)); - var field = new ComPortField(new PrinterConfig(null, PrinterSettings.Empty)); + var field = new ComPortField(new PrinterConfig(PrinterSettings.Empty)); await ValidateAgainstValueMap( field,