diff --git a/Library/Provider/LibraryProvider.cs b/Library/Provider/LibraryProvider.cs index 518e3a8d4..0c55dfa06 100644 --- a/Library/Provider/LibraryProvider.cs +++ b/Library/Provider/LibraryProvider.cs @@ -97,7 +97,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider public abstract void AddCollectionToLibrary(string collectionName); - public abstract void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null); + public abstract void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null); public abstract void AddItem(PrintItemWrapper itemToAdd); diff --git a/Library/Provider/LibraryProviderFileSystem.cs b/Library/Provider/LibraryProviderFileSystem.cs index 0e4e704db..54345b67f 100644 --- a/Library/Provider/LibraryProviderFileSystem.cs +++ b/Library/Provider/LibraryProviderFileSystem.cs @@ -136,7 +136,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider } } - public override void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null) + public override void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null) { string destPath = rootPath; diff --git a/Library/Provider/LibraryProviderSelector.cs b/Library/Provider/LibraryProviderSelector.cs index 37f6d0122..96ca2ae15 100644 --- a/Library/Provider/LibraryProviderSelector.cs +++ b/Library/Provider/LibraryProviderSelector.cs @@ -151,7 +151,7 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider throw new NotImplementedException(); } - public override void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null) + public override void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null) { throw new NotImplementedException(); } diff --git a/Library/Provider/LibraryProviderSqlite.cs b/Library/Provider/LibraryProviderSqlite.cs index 33d11b63f..ae77ef1a0 100644 --- a/Library/Provider/LibraryProviderSqlite.cs +++ b/Library/Provider/LibraryProviderSqlite.cs @@ -42,6 +42,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; +using System.Threading.Tasks; namespace MatterHackers.MatterControl.PrintLibrary.Provider { @@ -237,22 +238,21 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider LoadLibraryItems(); } - public override void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null, RunWorkerCompletedEventHandler callback = null) + public override async void AddFilesToLibrary(IList files, ReportProgressRatio reportProgress = null) { if (files != null && files.Count > 0) { - BackgroundWorker loadFilesIntoLibraryBackgroundWorker = new BackgroundWorker(); - loadFilesIntoLibraryBackgroundWorker.WorkerReportsProgress = true; + // create enough info to show that we have items pending (maybe use names from this file list for them) + // refresh the display to show the pending items + //LibraryProvider.OnDataReloaded(null); - loadFilesIntoLibraryBackgroundWorker.DoWork += new DoWorkEventHandler(loadFilesIntoLibraryBackgoundWorker_DoWork); - loadFilesIntoLibraryBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(loadFilesIntoLibraryBackgroundWorker_RunWorkerCompleted); + await Task.Run(() => loadFilesIntoLibraryBackgoundWorker_DoWork(files)); - if (callback != null) + if (baseLibraryCollection != null) { - loadFilesIntoLibraryBackgroundWorker.RunWorkerCompleted += callback; + LoadLibraryItems(); + LibraryProvider.OnDataReloaded(null); } - - loadFilesIntoLibraryBackgroundWorker.RunWorkerAsync(files); } } @@ -442,9 +442,8 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider return result; } - private void loadFilesIntoLibraryBackgoundWorker_DoWork(object sender, DoWorkEventArgs e) + private void loadFilesIntoLibraryBackgoundWorker_DoWork(IList fileList) { - IList fileList = e.Argument as IList; foreach (string loadedFileName in fileList) { string extension = Path.GetExtension(loadedFileName).ToUpper(); @@ -471,14 +470,5 @@ namespace MatterHackers.MatterControl.PrintLibrary.Provider } } } - - private void loadFilesIntoLibraryBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) - { - if (baseLibraryCollection != null) - { - LoadLibraryItems(); - LibraryProvider.OnDataReloaded(null); - } - } } } \ No newline at end of file diff --git a/MatterControl.csproj b/MatterControl.csproj index 9e77f992f..a20e71475 100644 --- a/MatterControl.csproj +++ b/MatterControl.csproj @@ -204,24 +204,24 @@ - - - - - - - - - - + + + + + + + + + + - + - - - - + + + + @@ -264,7 +264,7 @@ - + @@ -283,7 +283,7 @@ - + @@ -305,20 +305,20 @@ - - - - + + + + - - + + - - + + - + diff --git a/PartPreviewWindow/View3D/View3DAlign.cs b/PartPreviewWindow/View3D/View3DAlign.cs index cb2630145..aac67556d 100644 --- a/PartPreviewWindow/View3D/View3DAlign.cs +++ b/PartPreviewWindow/View3D/View3DAlign.cs @@ -114,7 +114,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow // create the selection info PlatingHelper.CreateITraceableForMeshGroup(asynchPlatingDatas, asynchMeshGroups, i, (double progress0To1, string processingState, out bool continueProcessing) => { - BackgroundWorker_ProgressChanged(progress0To1, processingState, out continueProcessing); + ReportProgressChanged(progress0To1, processingState, out continueProcessing); }); currentRatioDone += ratioPerMeshGroup; diff --git a/PartPreviewWindow/View3D/View3DAutoArange.cs b/PartPreviewWindow/View3D/View3DAutoArange.cs index 619353bd3..332121fa2 100644 --- a/PartPreviewWindow/View3D/View3DAutoArange.cs +++ b/PartPreviewWindow/View3D/View3DAutoArange.cs @@ -92,7 +92,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow for (int meshGroupIndex = 0; meshGroupIndex < asynchMeshGroups.Count; meshGroupIndex++) { bool continueProcessing2 = true; - BackgroundWorker_ProgressChanged(currentRatioDone, "Calculating Positions...".Localize(), out continueProcessing2); + ReportProgressChanged(currentRatioDone, "Calculating Positions...".Localize(), out continueProcessing2); MeshGroup meshGroup = asynchMeshGroups[meshGroupIndex]; Vector3 meshLowerLeft = meshGroup.GetAxisAlignedBoundingBox(asynchMeshGroupTransforms[meshGroupIndex].TotalTransform).minXYZ; diff --git a/PartPreviewWindow/View3D/View3DCopyGroup.cs b/PartPreviewWindow/View3D/View3DCopyGroup.cs index ea1c16ece..20d04bbf8 100644 --- a/PartPreviewWindow/View3D/View3DCopyGroup.cs +++ b/PartPreviewWindow/View3D/View3DCopyGroup.cs @@ -32,15 +32,15 @@ using MatterHackers.PolygonMesh; using System.ComponentModel; using System.Globalization; using System.Threading; +using System.Threading.Tasks; namespace MatterHackers.MatterControl.PartPreviewWindow { public partial class View3DWidget { - private void copyGroupBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) + private void copyGroupBackgroundWorker_DoWork() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; - BackgroundWorker backgroundWorker = (BackgroundWorker)sender; PushMeshGroupDataToAsynchLists(TraceInfoOpperation.DO_COPY); @@ -52,7 +52,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow Mesh mesh = asynchMeshGroups[SelectedMeshGroupIndex].Meshes[i]; copyMeshGroup.Meshes.Add(Mesh.Copy(mesh, (double progress0To1, string processingState, out bool continueProcessing) => { - BackgroundWorker_ProgressChanged(progress0To1, processingState, out continueProcessing); + ReportProgressChanged(progress0To1, processingState, out continueProcessing); })); } @@ -60,26 +60,10 @@ namespace MatterHackers.MatterControl.PartPreviewWindow PlatingHelper.CreateITraceableForMeshGroup(asynchPlatingDatas, asynchMeshGroups, asynchMeshGroups.Count - 1, null); bool continueProcessing2; - BackgroundWorker_ProgressChanged(.95, "", out continueProcessing2); + ReportProgressChanged(.95, "", out continueProcessing2); } - private void copyGroupBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) - { - if (WidgetHasBeenClosed) - { - return; - } - - UnlockEditControls(); - PullMeshGroupDataFromAsynchLists(); - PartHasBeenChanged(); - - // now set the selection to the new copy - MeshGroupExtraData[MeshGroups.Count - 1].currentScale = MeshGroupExtraData[SelectedMeshGroupIndex].currentScale; - SelectedMeshGroupIndex = MeshGroups.Count - 1; - } - - private void MakeCopyOfGroup() + private async void MakeCopyOfGroup() { if (MeshGroups.Count > 0 && SelectedMeshGroupIndex != -1) @@ -91,13 +75,20 @@ namespace MatterHackers.MatterControl.PartPreviewWindow processingProgressControl.PercentComplete = 0; LockEditControls(); - BackgroundWorker copyGroupBackgroundWorker = null; - copyGroupBackgroundWorker = new BackgroundWorker(); + await Task.Run(() => copyGroupBackgroundWorker_DoWork()); - copyGroupBackgroundWorker.DoWork += new DoWorkEventHandler(copyGroupBackgroundWorker_DoWork); - copyGroupBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(copyGroupBackgroundWorker_RunWorkerCompleted); + if (WidgetHasBeenClosed) + { + return; + } - copyGroupBackgroundWorker.RunWorkerAsync(); + UnlockEditControls(); + PullMeshGroupDataFromAsynchLists(); + PartHasBeenChanged(); + + // now set the selection to the new copy + MeshGroupExtraData[MeshGroups.Count - 1].currentScale = MeshGroupExtraData[SelectedMeshGroupIndex].currentScale; + SelectedMeshGroupIndex = MeshGroups.Count - 1; } } } diff --git a/PartPreviewWindow/View3D/View3DCreateSelecitonData.cs b/PartPreviewWindow/View3D/View3DCreateSelecitonData.cs index 79ea0f070..e13eadc59 100644 --- a/PartPreviewWindow/View3D/View3DCreateSelecitonData.cs +++ b/PartPreviewWindow/View3D/View3DCreateSelecitonData.cs @@ -62,14 +62,14 @@ namespace MatterHackers.MatterControl.PartPreviewWindow // create the selection info PlatingHelper.CreateITraceableForMeshGroup(asynchPlatingDatas, asynchMeshGroups, i, (double progress0To1, string processingState, out bool continueProcessing) => { - BackgroundWorker_ProgressChanged(progress0To1, processingState, out continueProcessing); + ReportProgressChanged(progress0To1, processingState, out continueProcessing); }); currentRatioDone += ratioPerMeshGroup; } bool continueProcessing2; - BackgroundWorker_ProgressChanged(1, "Creating GL Data", out continueProcessing2); + ReportProgressChanged(1, "Creating GL Data", out continueProcessing2); meshViewerWidget.CreateGlDataForMeshes(asynchMeshGroups); } diff --git a/PartPreviewWindow/View3D/View3DGroup.cs b/PartPreviewWindow/View3D/View3DGroup.cs index e5fc02053..e1bf97a7a 100644 --- a/PartPreviewWindow/View3D/View3DGroup.cs +++ b/PartPreviewWindow/View3D/View3DGroup.cs @@ -78,7 +78,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow // create the selection info PlatingHelper.CreateITraceableForMeshGroup(asynchPlatingDatas, asynchMeshGroups, i, (double progress0To1, string processingState, out bool continueProcessing) => { - BackgroundWorker_ProgressChanged(progress0To1, processingState, out continueProcessing); + ReportProgressChanged(progress0To1, processingState, out continueProcessing); }); currentRatioDone += ratioPerMeshGroup; @@ -101,7 +101,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow asynchMeshGroups[i].Transform(asynchMeshGroupTransforms[i].TotalTransform); bool continueProcessing; - BackgroundWorker_ProgressChanged((i + 1) * .4 / asynchMeshGroups.Count, "", out continueProcessing); + ReportProgressChanged((i + 1) * .4 / asynchMeshGroups.Count, "", out continueProcessing); } DoGroup(backgroundWorker); diff --git a/PartPreviewWindow/View3D/View3DUngroup.cs b/PartPreviewWindow/View3D/View3DUngroup.cs index d555ff2cc..39e882547 100644 --- a/PartPreviewWindow/View3D/View3DUngroup.cs +++ b/PartPreviewWindow/View3D/View3DUngroup.cs @@ -69,7 +69,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow { discreetMeshes = CreateDiscreteMeshes.SplitConnectedIntoMeshes(asynchMeshGroups[indexBeingReplaced], (double progress0To1, string processingState, out bool continueProcessing) => { - BackgroundWorker_ProgressChanged(progress0To1 * .5, processingState, out continueProcessing); + ReportProgressChanged(progress0To1 * .5, processingState, out continueProcessing); }); } @@ -95,7 +95,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow // and create selection info PlatingHelper.CreateITraceableForMeshGroup(asynchPlatingDatas, asynchMeshGroups, addedMeshIndex, (double progress0To1, string processingState, out bool continueProcessing) => { - BackgroundWorker_ProgressChanged(.5 + progress0To1 * .5 * currentRatioDone, processingState, out continueProcessing); + ReportProgressChanged(.5 + progress0To1 * .5 * currentRatioDone, processingState, out continueProcessing); }); currentRatioDone += ratioPerDiscreetMesh; } diff --git a/PartPreviewWindow/View3D/View3DWidget.cs b/PartPreviewWindow/View3D/View3DWidget.cs index 3e04642ce..a3bee242f 100644 --- a/PartPreviewWindow/View3D/View3DWidget.cs +++ b/PartPreviewWindow/View3D/View3DWidget.cs @@ -54,6 +54,7 @@ using System.Diagnostics; using System.Globalization; using System.IO; using System.Threading; +using System.Threading.Tasks; namespace MatterHackers.MatterControl.PartPreviewWindow { @@ -1093,7 +1094,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow } } - private void BackgroundWorker_ProgressChanged(double progress0To1, string processingState, out bool continueProcessing) + private void ReportProgressChanged(double progress0To1, string processingState, out bool continueProcessing) { if (!timeSinceReported.IsRunning || timeSinceReported.ElapsedMilliseconds > 100 || processingState != processingProgressControl.ProgressMessage) @@ -1619,7 +1620,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow return horizontalRule; } - private void LoadAndAddPartsToPlate(string[] filesToLoad) + private async void LoadAndAddPartsToPlate(string[] filesToLoad) { if (MeshGroups.Count > 0 && filesToLoad != null && filesToLoad.Length > 0) { @@ -1632,22 +1633,34 @@ namespace MatterHackers.MatterControl.PartPreviewWindow PushMeshGroupDataToAsynchLists(TraceInfoOpperation.DO_COPY); - BackgroundWorker loadAndAddPartsToPlateBackgroundWorker = null; - loadAndAddPartsToPlateBackgroundWorker = new BackgroundWorker(); + await Task.Run(() => loadAndAddPartsToPlate(filesToLoad)); - loadAndAddPartsToPlateBackgroundWorker.DoWork += new DoWorkEventHandler(loadAndAddPartsToPlateBackgroundWorker_DoWork); - loadAndAddPartsToPlateBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(loadAndAddPartsToPlateBackgroundWorker_RunWorkerCompleted); + if (WidgetHasBeenClosed) + { + return; + } - loadAndAddPartsToPlateBackgroundWorker.RunWorkerAsync(filesToLoad); + UnlockEditControls(); + PartHasBeenChanged(); + + bool addingOnlyOneItem = asynchMeshGroups.Count == MeshGroups.Count + 1; + + if (MeshGroups.Count > 0) + { + PullMeshGroupDataFromAsynchLists(); + if (addingOnlyOneItem) + { + // if we are only adding one part to the plate set the selection to it + SelectedMeshGroupIndex = asynchMeshGroups.Count - 1; + } + } } } - private void loadAndAddPartsToPlateBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) + private void loadAndAddPartsToPlate(string[] filesToLoadIncludingZips) { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; - BackgroundWorker backgroundWorker = (BackgroundWorker)sender; - string[] filesToLoadIncludingZips = e.Argument as string[]; List filesToLoad = new List(); if (filesToLoadIncludingZips != null && filesToLoadIncludingZips.Length > 0) { @@ -1684,7 +1697,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow continueProcessing = !this.WidgetHasBeenClosed; double ratioAvailable = (ratioPerFile * .5); double currentRatio = currentRatioDone + progress0To1 * ratioAvailable; - BackgroundWorker_ProgressChanged(currentRatio, progressMessage, out continueProcessing); + ReportProgressChanged(currentRatio, progressMessage, out continueProcessing); }); if (WidgetHasBeenClosed) @@ -1711,7 +1724,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow double ratioAvailable = (ratioPerFile * .5); // done outer loop + done this loop +first 1/2 (load)+ this part * ratioAvailable double currentRatio = currentRatioDone + subMeshRatioDone + ratioAvailable + progress0To1 * ratioPerSubMesh; - BackgroundWorker_ProgressChanged(currentRatio, progressMessage, out continueProcessing); + ReportProgressChanged(currentRatio, progressMessage, out continueProcessing); }); subMeshRatioDone += ratioPerSubMesh; @@ -1723,28 +1736,6 @@ namespace MatterHackers.MatterControl.PartPreviewWindow } } - private void loadAndAddPartsToPlateBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) - { - if (WidgetHasBeenClosed) - { - return; - } - UnlockEditControls(); - PartHasBeenChanged(); - - bool addingOnlyOneItem = asynchMeshGroups.Count == MeshGroups.Count + 1; - - if (MeshGroups.Count > 0) - { - PullMeshGroupDataFromAsynchLists(); - if (addingOnlyOneItem) - { - // if we are only adding one part to the plate set the selection to it - SelectedMeshGroupIndex = asynchMeshGroups.Count - 1; - } - } - } - private void LockEditControls() { viewIsInEditModePreLock = doEdittingButtonsContainer.Visible; @@ -1866,7 +1857,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow asynchMeshGroups[i].Transform(asynchMeshGroupTransforms[i].TotalTransform); bool continueProcessing; - BackgroundWorker_ProgressChanged((i + 1) * .4 / asynchMeshGroups.Count, "", out continueProcessing); + ReportProgressChanged((i + 1) * .4 / asynchMeshGroups.Count, "", out continueProcessing); } saveSucceded = true;