Move heavy tasks out of constructor, make awaitable

- Add support for awaitable startup
- Issue MatterHackers/MCCentral#2423
Select printer not changing to new printer
This commit is contained in:
John Lewin 2017-12-20 18:25:12 -08:00
parent e4e5e59794
commit 4415300a1f
5 changed files with 66 additions and 67 deletions

View file

@ -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<PrinterConfig> ActivePrinters { get; } = new List<PrinterConfig>();
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<double, string> reporter)
public static async Task<GuiWidget> Initialize(SystemWindow systemWindow, Action<double, string> reporter)
{
AppContext.Platform = AggContext.CreateInstanceFrom<INativePlatformFeatures>(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();

View file

@ -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)
/// <summary>
/// Loads content to the bed and prepares the printer for use
/// </summary>
/// <param name="editContext"></param>
/// <returns></returns>
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)

View file

@ -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)

View file

@ -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; }
/// <summary>
@ -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);
}
/// <summary>
@ -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);

View file

@ -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,