diff --git a/ActionBar/PrintStatusRow.cs b/ActionBar/PrintStatusRow.cs index 1350c04a5..6106dd035 100644 --- a/ActionBar/PrintStatusRow.cs +++ b/ActionBar/PrintStatusRow.cs @@ -113,6 +113,7 @@ namespace MatterHackers.MatterControl.ActionBar public override void OnClosed(EventArgs e) { + activePrintPreviewImage.PrintItem.SlicingOutputMessage -= PrintItem_SlicingOutputMessage; if (unregisterEvents != null) { unregisterEvents(this, null); @@ -299,7 +300,7 @@ namespace MatterHackers.MatterControl.ActionBar // first we have to remove any link to an old part (the part currently in the view) if (activePrintPreviewImage.PrintItem != null) { - activePrintPreviewImage.PrintItem.SlicingOutputMessage.UnregisterEvent(PrintItem_SlicingOutputMessage, ref unregisterEvents); + activePrintPreviewImage.PrintItem.SlicingOutputMessage -= PrintItem_SlicingOutputMessage; } activePrintPreviewImage.PrintItem = PrinterConnectionAndCommunication.Instance.ActivePrintItem; @@ -307,7 +308,7 @@ namespace MatterHackers.MatterControl.ActionBar // then hook up our new part if (activePrintPreviewImage.PrintItem != null) { - activePrintPreviewImage.PrintItem.SlicingOutputMessage.RegisterEvent(PrintItem_SlicingOutputMessage, ref unregisterEvents); + activePrintPreviewImage.PrintItem.SlicingOutputMessage += PrintItem_SlicingOutputMessage; } activePrintPreviewImage.Invalidate(); diff --git a/CustomWidgets/ExportPrintItemWindow.cs b/CustomWidgets/ExportPrintItemWindow.cs index f523e3367..37083ef16 100644 --- a/CustomWidgets/ExportPrintItemWindow.cs +++ b/CustomWidgets/ExportPrintItemWindow.cs @@ -242,7 +242,7 @@ namespace MatterHackers.MatterControl { SlicingQueue.Instance.QueuePartForSlicing(printItemWrapper); - printItemWrapper.SlicingDone.RegisterEvent(sliceItem_Done, ref unregisterEvents); + printItemWrapper.SlicingDone += sliceItem_Done; } else if (partIsGCode) @@ -278,7 +278,7 @@ namespace MatterHackers.MatterControl { Close(); SlicingQueue.Instance.QueuePartForSlicing(printItemWrapper); - printItemWrapper.SlicingDone.RegisterEvent(x3gItemSlice_Complete, ref unregisterEvents); + printItemWrapper.SlicingDone += x3gItemSlice_Complete; } else if (partIsGCode) { @@ -352,6 +352,7 @@ namespace MatterHackers.MatterControl public override void OnClosed(EventArgs e) { + printItemWrapper.SlicingDone -= sliceItem_Done; if (unregisterEvents != null) { unregisterEvents(this, null); @@ -469,14 +470,14 @@ namespace MatterHackers.MatterControl { PrintItemWrapper sliceItem = (PrintItemWrapper)sender; - printItemWrapper.SlicingDone.UnregisterEvent(sliceItem_Done, ref unregisterEvents); + printItemWrapper.SlicingDone -= sliceItem_Done; SaveGCodeToNewLocation(sliceItem.GetGCodePathAndFileName(), gcodePathAndFilenameToSave); } private void x3gItemSlice_Complete(object sender, EventArgs e) { PrintItemWrapper sliceItem = (PrintItemWrapper)sender; - printItemWrapper.SlicingDone.UnregisterEvent(x3gItemSlice_Complete, ref unregisterEvents); + printItemWrapper.SlicingDone -= x3gItemSlice_Complete; if (File.Exists(sliceItem.GetGCodePathAndFileName())) { generateX3GfromGcode(sliceItem.GetGCodePathAndFileName(), x3gPathAndFilenameToSave); diff --git a/CustomWidgets/PartThumbnailWidget.cs b/CustomWidgets/PartThumbnailWidget.cs index 1207f4a6a..68d8c9c5f 100644 --- a/CustomWidgets/PartThumbnailWidget.cs +++ b/CustomWidgets/PartThumbnailWidget.cs @@ -143,13 +143,13 @@ namespace MatterHackers.MatterControl { if (printItem != null) { - printItem.FileHasChanged.UnregisterEvent(item_FileHasChanged, ref unregisterEvents); + printItem.FileHasChanged -= item_FileHasChanged; } printItem = value; thumbNailHasBeenCreated = false; if (printItem != null) { - printItem.FileHasChanged.RegisterEvent(item_FileHasChanged, ref unregisterEvents); + printItem.FileHasChanged += item_FileHasChanged; } } } @@ -172,7 +172,7 @@ namespace MatterHackers.MatterControl } if (printItem != null) { - printItem.FileHasChanged.UnregisterEvent(item_FileHasChanged, ref unregisterEvents); + printItem.FileHasChanged -= item_FileHasChanged; } base.OnClosed(e); } diff --git a/PartPreviewWindow/View3D/View3DAlign.cs b/PartPreviewWindow/View3D/View3DAlign.cs index aac67556d..e636a976e 100644 --- a/PartPreviewWindow/View3D/View3DAlign.cs +++ b/PartPreviewWindow/View3D/View3DAlign.cs @@ -34,12 +34,13 @@ using MatterHackers.VectorMath; using System.ComponentModel; using System.Globalization; using System.Threading; +using System.Threading.Tasks; namespace MatterHackers.MatterControl.PartPreviewWindow { public partial class View3DWidget { - private void alignSelectedBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) + private void AlignSelected() { if (SelectedMeshGroupIndex == -1) { @@ -47,7 +48,6 @@ namespace MatterHackers.MatterControl.PartPreviewWindow } // make sure our thread traslates numbmers correctly (always do this in a thread) Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; - BackgroundWorker backgroundWorker = (BackgroundWorker)sender; // save our data so we don't mess up the display while doing work PushMeshGroupDataToAsynchLists(TraceInfoOpperation.DO_COPY); @@ -121,25 +121,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow } } - private void alignSelectedBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) - { - if (WidgetHasBeenClosed) - { - return; - } - - // remove the original mesh and replace it with these new meshes - PullMeshGroupDataFromAsynchLists(); - - // our selection changed to the mesh we just added which is at the end - SelectedMeshGroupIndex = MeshGroups.Count - 1; - - UnlockEditControls(); - - Invalidate(); - } - - private void AlignToSelectedMeshGroup() + private async void AlignToSelectedMeshGroup() { if (MeshGroups.Count > 0) { @@ -153,13 +135,22 @@ namespace MatterHackers.MatterControl.PartPreviewWindow LockEditControls(); viewIsInEditModePreLock = true; - BackgroundWorker createDiscreteMeshesBackgroundWorker = null; - createDiscreteMeshesBackgroundWorker = new BackgroundWorker(); + await Task.Run(() => AlignSelected()); - createDiscreteMeshesBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(alignSelectedBackgroundWorker_RunWorkerCompleted); - createDiscreteMeshesBackgroundWorker.DoWork += new DoWorkEventHandler(alignSelectedBackgroundWorker_DoWork); + if (WidgetHasBeenClosed) + { + return; + } - createDiscreteMeshesBackgroundWorker.RunWorkerAsync(); + // remove the original mesh and replace it with these new meshes + PullMeshGroupDataFromAsynchLists(); + + // our selection changed to the mesh we just added which is at the end + SelectedMeshGroupIndex = MeshGroups.Count - 1; + + UnlockEditControls(); + + Invalidate(); } } } diff --git a/PartPreviewWindow/View3D/View3DAutoArange.cs b/PartPreviewWindow/View3D/View3DAutoArange.cs index 332121fa2..db6231b6a 100644 --- a/PartPreviewWindow/View3D/View3DAutoArange.cs +++ b/PartPreviewWindow/View3D/View3DAutoArange.cs @@ -35,18 +35,17 @@ using System; using System.ComponentModel; using System.Globalization; using System.Threading; +using System.Threading.Tasks; namespace MatterHackers.MatterControl.PartPreviewWindow { public partial class View3DWidget { - private void arrangeMeshGroupsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) + private void ArrangeMeshGroups() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; PushMeshGroupDataToAsynchLists(TraceInfoOpperation.DONT_COPY); - BackgroundWorker backgroundWorker = (BackgroundWorker)sender; - // move them all out of the way for (int i = 0; i < asynchMeshGroups.Count; i++) { @@ -132,19 +131,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow } } - private void arrangeMeshGroupsBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) - { - if (WidgetHasBeenClosed) - { - return; - } - UnlockEditControls(); - PartHasBeenChanged(); - - PullMeshGroupDataFromAsynchLists(); - } - - private void AutoArrangePartsInBackground() + private async void AutoArrangePartsInBackground() { if (MeshGroups.Count > 0) { @@ -155,12 +142,16 @@ namespace MatterHackers.MatterControl.PartPreviewWindow processingProgressControl.PercentComplete = 0; LockEditControls(); - BackgroundWorker arrangeMeshGroupsBackgroundWorker = new BackgroundWorker(); + await Task.Run(() => ArrangeMeshGroups()); - arrangeMeshGroupsBackgroundWorker.DoWork += new DoWorkEventHandler(arrangeMeshGroupsBackgroundWorker_DoWork); - arrangeMeshGroupsBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(arrangeMeshGroupsBackgroundWorker_RunWorkerCompleted); + if (WidgetHasBeenClosed) + { + return; + } + UnlockEditControls(); + PartHasBeenChanged(); - arrangeMeshGroupsBackgroundWorker.RunWorkerAsync(); + PullMeshGroupDataFromAsynchLists(); } } } diff --git a/PartPreviewWindow/View3D/View3DCopyGroup.cs b/PartPreviewWindow/View3D/View3DCopyGroup.cs index 20d04bbf8..67ecab778 100644 --- a/PartPreviewWindow/View3D/View3DCopyGroup.cs +++ b/PartPreviewWindow/View3D/View3DCopyGroup.cs @@ -38,7 +38,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow { public partial class View3DWidget { - private void copyGroupBackgroundWorker_DoWork() + private void CopyGroup() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; @@ -75,7 +75,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow processingProgressControl.PercentComplete = 0; LockEditControls(); - await Task.Run(() => copyGroupBackgroundWorker_DoWork()); + await Task.Run(() => CopyGroup()); if (WidgetHasBeenClosed) { diff --git a/PartPreviewWindow/View3D/View3DCreateSelecitonData.cs b/PartPreviewWindow/View3D/View3DCreateSelecitonData.cs index e13eadc59..d92eb8097 100644 --- a/PartPreviewWindow/View3D/View3DCreateSelecitonData.cs +++ b/PartPreviewWindow/View3D/View3DCreateSelecitonData.cs @@ -33,19 +33,19 @@ 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 createSelectionDataBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) + private void CreateSelectionData() { string makingCopyLabel = LocalizedString.Get("Preparing Meshes"); string makingCopyLabelFull = string.Format("{0}:", makingCopyLabel); processingProgressControl.ProcessType = makingCopyLabelFull; Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; - BackgroundWorker backgroundWorker = (BackgroundWorker)sender; PushMeshGroupDataToAsynchLists(TraceInfoOpperation.DONT_COPY); @@ -73,39 +73,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow meshViewerWidget.CreateGlDataForMeshes(asynchMeshGroups); } - private void createSelectionDataBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) - { - if (WidgetHasBeenClosed) - { - return; - } - // remove the original mesh and replace it with these new meshes - PullMeshGroupDataFromAsynchLists(); - - SelectedMeshGroupIndex = 0; - buttonRightPanel.Visible = true; - UnlockEditControls(); - viewControls3D.partSelectButton.ClickButton(null); - - Invalidate(); - - if (DoAddFileAfterCreatingEditData) - { - FileDialog.OpenFileDialog( - new OpenFileDialogParams(ApplicationSettings.OpenDesignFileParams, multiSelect: true), - (openParams) => - { - LoadAndAddPartsToPlate(openParams.FileNames); - }); - DoAddFileAfterCreatingEditData = false; - } - else if (pendingPartsToLoad.Count > 0) - { - LoadAndAddPartsToPlate(pendingPartsToLoad.ToArray()); - } - } - - private void EnterEditAndCreateSelectionData() + private async void EnterEditAndCreateSelectionData() { if (enterEditButtonsContainer.Visible == true) { @@ -118,13 +86,36 @@ namespace MatterHackers.MatterControl.PartPreviewWindow LockEditControls(); viewIsInEditModePreLock = true; - BackgroundWorker createSelectionDataBackgroundWorker = null; - createSelectionDataBackgroundWorker = new BackgroundWorker(); + await Task.Run(() => CreateSelectionData()); - createSelectionDataBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(createSelectionDataBackgroundWorker_RunWorkerCompleted); - createSelectionDataBackgroundWorker.DoWork += new DoWorkEventHandler(createSelectionDataBackgroundWorker_DoWork); + if (WidgetHasBeenClosed) + { + return; + } + // remove the original mesh and replace it with these new meshes + PullMeshGroupDataFromAsynchLists(); - createSelectionDataBackgroundWorker.RunWorkerAsync(); + SelectedMeshGroupIndex = 0; + buttonRightPanel.Visible = true; + UnlockEditControls(); + viewControls3D.partSelectButton.ClickButton(null); + + Invalidate(); + + if (DoAddFileAfterCreatingEditData) + { + FileDialog.OpenFileDialog( + new OpenFileDialogParams(ApplicationSettings.OpenDesignFileParams, multiSelect: true), + (openParams) => + { + LoadAndAddPartsToPlate(openParams.FileNames); + }); + DoAddFileAfterCreatingEditData = false; + } + else if (pendingPartsToLoad.Count > 0) + { + LoadAndAddPartsToPlate(pendingPartsToLoad.ToArray()); + } } } } diff --git a/PartPreviewWindow/View3D/View3DGroup.cs b/PartPreviewWindow/View3D/View3DGroup.cs index e1bf97a7a..e0011ea6a 100644 --- a/PartPreviewWindow/View3D/View3DGroup.cs +++ b/PartPreviewWindow/View3D/View3DGroup.cs @@ -33,17 +33,35 @@ 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 DoGroup(BackgroundWorker backgroundWorker) + private void GroupSelected() { + string makingCopyLabel = LocalizedString.Get("Grouping"); + string makingCopyLabelFull = string.Format("{0}:", makingCopyLabel); + processingProgressControl.ProcessType = makingCopyLabelFull; + + Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; + + PushMeshGroupDataToAsynchLists(TraceInfoOpperation.DO_COPY); + + for (int i = 0; i < asynchMeshGroups.Count; i++) + { + asynchMeshGroups[i].Transform(asynchMeshGroupTransforms[i].TotalTransform); + + bool continueProcessing; + ReportProgressChanged((i + 1) * .4 / asynchMeshGroups.Count, "", out continueProcessing); + } + if (SelectedMeshGroupIndex == -1) { SelectedMeshGroupIndex = 0; } + MeshGroup meshGroupWeAreKeeping = asynchMeshGroups[SelectedMeshGroupIndex]; for (int meshGroupToMoveIndex = asynchMeshGroups.Count - 1; meshGroupToMoveIndex >= 0; meshGroupToMoveIndex--) { @@ -85,49 +103,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow } } - private void groupSelectedBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) - { - string makingCopyLabel = LocalizedString.Get("Grouping"); - string makingCopyLabelFull = string.Format("{0}:", makingCopyLabel); - processingProgressControl.ProcessType = makingCopyLabelFull; - - Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; - BackgroundWorker backgroundWorker = (BackgroundWorker)sender; - - PushMeshGroupDataToAsynchLists(TraceInfoOpperation.DO_COPY); - - for (int i = 0; i < asynchMeshGroups.Count; i++) - { - asynchMeshGroups[i].Transform(asynchMeshGroupTransforms[i].TotalTransform); - - bool continueProcessing; - ReportProgressChanged((i + 1) * .4 / asynchMeshGroups.Count, "", out continueProcessing); - } - - DoGroup(backgroundWorker); - } - - private void groupSelectedBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) - { - if (WidgetHasBeenClosed) - { - return; - } - - // remove the original mesh and replace it with these new meshes - PullMeshGroupDataFromAsynchLists(); - - // our selection changed to the mesh we just added which is at the end - SelectedMeshGroupIndex = MeshGroups.Count - 1; - - UnlockEditControls(); - - PartHasBeenChanged(); - - Invalidate(); - } - - private void GroupSelectedMeshs() + private async void GroupSelectedMeshs() { if (MeshGroups.Count > 0) { @@ -136,13 +112,24 @@ namespace MatterHackers.MatterControl.PartPreviewWindow LockEditControls(); viewIsInEditModePreLock = true; - BackgroundWorker createDiscreteMeshesBackgroundWorker = null; - createDiscreteMeshesBackgroundWorker = new BackgroundWorker(); + await Task.Run(() => GroupSelected()); - createDiscreteMeshesBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(groupSelectedBackgroundWorker_RunWorkerCompleted); - createDiscreteMeshesBackgroundWorker.DoWork += new DoWorkEventHandler(groupSelectedBackgroundWorker_DoWork); + if (WidgetHasBeenClosed) + { + return; + } - createDiscreteMeshesBackgroundWorker.RunWorkerAsync(); + // remove the original mesh and replace it with these new meshes + PullMeshGroupDataFromAsynchLists(); + + // our selection changed to the mesh we just added which is at the end + SelectedMeshGroupIndex = MeshGroups.Count - 1; + + UnlockEditControls(); + + PartHasBeenChanged(); + + Invalidate(); } } } diff --git a/PartPreviewWindow/View3D/View3DUngroup.cs b/PartPreviewWindow/View3D/View3DUngroup.cs index 39e882547..e14c65faf 100644 --- a/PartPreviewWindow/View3D/View3DUngroup.cs +++ b/PartPreviewWindow/View3D/View3DUngroup.cs @@ -34,12 +34,13 @@ using System.Collections.Generic; using System.ComponentModel; using System.Globalization; using System.Threading; +using System.Threading.Tasks; namespace MatterHackers.MatterControl.PartPreviewWindow { public partial class View3DWidget { - private void ungroupSelectedBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) + private void UngroupSelected() { if (SelectedMeshGroupIndex == -1) { @@ -50,7 +51,6 @@ namespace MatterHackers.MatterControl.PartPreviewWindow processingProgressControl.ProcessType = makingCopyLabelFull; Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; - BackgroundWorker backgroundWorker = (BackgroundWorker)sender; PushMeshGroupDataToAsynchLists(TraceInfoOpperation.DO_COPY); @@ -101,27 +101,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow } } - private void ungroupSelectedBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) - { - if (WidgetHasBeenClosed) - { - return; - } - - // remove the original mesh and replace it with these new meshes - PullMeshGroupDataFromAsynchLists(); - - // our selection changed to the mesh we just added which is at the end - SelectedMeshGroupIndex = MeshGroups.Count - 1; - - UnlockEditControls(); - - PartHasBeenChanged(); - - Invalidate(); - } - - private void UngroupSelectedMeshGroup() + private async void UngroupSelectedMeshGroup() { if (MeshGroups.Count > 0) { @@ -130,13 +110,24 @@ namespace MatterHackers.MatterControl.PartPreviewWindow LockEditControls(); viewIsInEditModePreLock = true; - BackgroundWorker createDiscreteMeshesBackgroundWorker = null; - createDiscreteMeshesBackgroundWorker = new BackgroundWorker(); + await Task.Run(() => UngroupSelected()); - createDiscreteMeshesBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ungroupSelectedBackgroundWorker_RunWorkerCompleted); - createDiscreteMeshesBackgroundWorker.DoWork += new DoWorkEventHandler(ungroupSelectedBackgroundWorker_DoWork); + if (WidgetHasBeenClosed) + { + return; + } - createDiscreteMeshesBackgroundWorker.RunWorkerAsync(); + // remove the original mesh and replace it with these new meshes + PullMeshGroupDataFromAsynchLists(); + + // our selection changed to the mesh we just added which is at the end + SelectedMeshGroupIndex = MeshGroups.Count - 1; + + UnlockEditControls(); + + PartHasBeenChanged(); + + Invalidate(); } } } diff --git a/PartPreviewWindow/View3D/View3DWidget.cs b/PartPreviewWindow/View3D/View3DWidget.cs index 0ffe0f485..8b4b3adbf 100644 --- a/PartPreviewWindow/View3D/View3DWidget.cs +++ b/PartPreviewWindow/View3D/View3DWidget.cs @@ -497,6 +497,7 @@ namespace MatterHackers.MatterControl.PartPreviewWindow private bool DoAddFileAfterCreatingEditData { get; set; } public override void OnClosed(EventArgs e) { + printItemWrapper.FileHasChanged -= ReloadMeshIfChangeExternaly; if (unregisterEvents != null) { unregisterEvents(this, null); @@ -1137,8 +1138,8 @@ namespace MatterHackers.MatterControl.PartPreviewWindow if (printItemWrapper != null) { // remove it first to make sure we don't double add it - printItemWrapper.FileHasChanged.UnregisterEvent(ReloadMeshIfChangeExternaly, ref unregisterEvents); - printItemWrapper.FileHasChanged.RegisterEvent(ReloadMeshIfChangeExternaly, ref unregisterEvents); + printItemWrapper.FileHasChanged -= ReloadMeshIfChangeExternaly; + printItemWrapper.FileHasChanged += ReloadMeshIfChangeExternaly; // Controls if the part should be automattically centered. Ideally, we should autocenter any time a user has // not moved parts around on the bed (as we do now) but skip autocentering if the user has moved and placed diff --git a/PartPreviewWindow/ViewGcodeBasic.cs b/PartPreviewWindow/ViewGcodeBasic.cs index 055327cb9..abb8ed6cf 100644 --- a/PartPreviewWindow/ViewGcodeBasic.cs +++ b/PartPreviewWindow/ViewGcodeBasic.cs @@ -221,8 +221,8 @@ namespace MatterHackers.MatterControl.PartPreviewWindow } // we only hook these up to make sure we can regenerate the gcode when we want - printItem.SlicingOutputMessage.RegisterEvent(sliceItem_SlicingOutputMessage, ref unregisterEvents); - printItem.SlicingDone.RegisterEvent(sliceItem_Done, ref unregisterEvents); + printItem.SlicingOutputMessage += sliceItem_SlicingOutputMessage; + printItem.SlicingDone += sliceItem_Done; } else { @@ -644,8 +644,8 @@ namespace MatterHackers.MatterControl.PartPreviewWindow if (PrinterConnectionAndCommunication.Instance.PrinterIsPrinting && PrinterConnectionAndCommunication.Instance.ActivePrintItem == printItem) { - printItem.SlicingOutputMessage.UnregisterEvent(sliceItem_SlicingOutputMessage, ref unregisterEvents); - printItem.SlicingDone.UnregisterEvent(sliceItem_Done, ref unregisterEvents); + printItem.SlicingOutputMessage -= sliceItem_SlicingOutputMessage; + printItem.SlicingDone -= sliceItem_Done; generateGCodeButton.Visible = false; @@ -718,12 +718,12 @@ namespace MatterHackers.MatterControl.PartPreviewWindow if (!PrinterConnectionAndCommunication.Instance.PrinterIsPaused && !PrinterConnectionAndCommunication.Instance.PrinterIsPrinting) { // unregister first to make sure we don't double up in error (should not be needed but no harm) - printItem.SlicingOutputMessage.UnregisterEvent(sliceItem_SlicingOutputMessage, ref unregisterEvents); - printItem.SlicingDone.UnregisterEvent(sliceItem_Done, ref unregisterEvents); + printItem.SlicingOutputMessage -= sliceItem_SlicingOutputMessage; + printItem.SlicingDone -= sliceItem_Done; // register for done slicing and slicing messages - printItem.SlicingOutputMessage.RegisterEvent(sliceItem_SlicingOutputMessage, ref unregisterEvents); - printItem.SlicingDone.RegisterEvent(sliceItem_Done, ref unregisterEvents); + printItem.SlicingOutputMessage += sliceItem_SlicingOutputMessage; + printItem.SlicingDone += sliceItem_Done; generateGCodeButton.Visible = true; } @@ -1034,8 +1034,8 @@ namespace MatterHackers.MatterControl.PartPreviewWindow if (printItem != null) { - printItem.SlicingOutputMessage.UnregisterEvent(sliceItem_SlicingOutputMessage, ref unregisterEvents); - printItem.SlicingDone.UnregisterEvent(sliceItem_Done, ref unregisterEvents); + printItem.SlicingOutputMessage -= sliceItem_SlicingOutputMessage; + printItem.SlicingDone -= sliceItem_Done; if (startedSliceFromGenerateButton && printItem.CurrentlySlicing) { SlicingQueue.Instance.CancelCurrentSlicing(); @@ -1078,8 +1078,8 @@ namespace MatterHackers.MatterControl.PartPreviewWindow // We can add this while we have it open (when we are done loading). // So we need to make sure we only have it added once. This will be ok to run when // not added or when added and will ensure we only have one hook. - printItem.SlicingOutputMessage.UnregisterEvent(sliceItem_SlicingOutputMessage, ref unregisterEvents); - printItem.SlicingDone.UnregisterEvent(sliceItem_Done, ref unregisterEvents); + printItem.SlicingOutputMessage -= sliceItem_SlicingOutputMessage; + printItem.SlicingDone -= sliceItem_Done; UiThread.RunOnIdle(CreateAndAddChildren); startedSliceFromGenerateButton = false; diff --git a/PrinterCommunication/PrinterConnectionAndCommunication.cs b/PrinterCommunication/PrinterConnectionAndCommunication.cs index d6fd89e50..1ad3823b9 100644 --- a/PrinterCommunication/PrinterConnectionAndCommunication.cs +++ b/PrinterCommunication/PrinterConnectionAndCommunication.cs @@ -282,8 +282,6 @@ namespace MatterHackers.MatterControl.PrinterCommunication } - private event EventHandler unregisterEvents; - [Flags] public enum Axis { X = 1, Y = 2, Z = 4, E = 8, XYZ = (X | Y | Z) } @@ -1327,7 +1325,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication PrinterConnectionAndCommunication.Instance.CommunicationState = PrinterConnectionAndCommunication.CommunicationStates.PreparingToPrint; PrintItemWrapper partToPrint = PrinterConnectionAndCommunication.Instance.ActivePrintItem; SlicingQueue.Instance.QueuePartForSlicing(partToPrint); - partToPrint.SlicingDone.RegisterEvent(partToPrint_SliceDone, ref unregisterEvents); + partToPrint.SlicingDone += partToPrint_SliceDone; } } else @@ -2521,7 +2519,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication PrinterConnectionAndCommunication.Instance.CommunicationState = PrinterConnectionAndCommunication.CommunicationStates.PreparingToPrint; PrintItemWrapper partToPrint = PrinterConnectionAndCommunication.Instance.ActivePrintItem; SlicingQueue.Instance.QueuePartForSlicing(partToPrint); - partToPrint.SlicingDone.RegisterEvent(partToPrint_SliceDone, ref unregisterEvents); + partToPrint.SlicingDone += partToPrint_SliceDone; } } @@ -2569,7 +2567,7 @@ namespace MatterHackers.MatterControl.PrinterCommunication PrintItemWrapper partToPrint = sender as PrintItemWrapper; if (partToPrint != null) { - partToPrint.SlicingDone.UnregisterEvent(partToPrint_SliceDone, ref unregisterEvents); + partToPrint.SlicingDone -= partToPrint_SliceDone; string gcodePathAndFileName = partToPrint.GetGCodePathAndFileName(); if (gcodePathAndFileName != "") { diff --git a/Queue/OptionsMenu/ExportToFolderProcess.cs b/Queue/OptionsMenu/ExportToFolderProcess.cs index eda9ef9d8..487794ba6 100644 --- a/Queue/OptionsMenu/ExportToFolderProcess.cs +++ b/Queue/OptionsMenu/ExportToFolderProcess.cs @@ -89,8 +89,6 @@ namespace MatterHackers.MatterControl.PrintQueue itemCountBeingWorkedOn = 0; } - private EventHandler unregisterEvents; - public void Start() { if (allFilesToExport.Count > 0) @@ -108,8 +106,8 @@ namespace MatterHackers.MatterControl.PrintQueue if (MeshFileIo.ValidFileExtensions().Contains(extension)) { SlicingQueue.Instance.QueuePartForSlicing(printItemWrapper); - printItemWrapper.SlicingDone.RegisterEvent(sliceItem_Done, ref unregisterEvents); - printItemWrapper.SlicingOutputMessage.RegisterEvent(printItemWrapper_SlicingOutputMessage, ref unregisterEvents); + printItemWrapper.SlicingDone += sliceItem_Done; + printItemWrapper.SlicingOutputMessage += printItemWrapper_SlicingOutputMessage; } else if (Path.GetExtension(part.FileLocation).ToUpper() == ".GCODE") { @@ -132,8 +130,9 @@ namespace MatterHackers.MatterControl.PrintQueue { PrintItemWrapper sliceItem = (PrintItemWrapper)sender; - sliceItem.SlicingDone.UnregisterEvent(sliceItem_Done, ref unregisterEvents); - sliceItem.SlicingOutputMessage.UnregisterEvent(printItemWrapper_SlicingOutputMessage, ref unregisterEvents); + sliceItem.SlicingDone -= sliceItem_Done; + sliceItem.SlicingOutputMessage -= printItemWrapper_SlicingOutputMessage; + if (File.Exists(sliceItem.FileLocation)) { savedGCodeFileNames.Add(sliceItem.GetGCodePathAndFileName()); diff --git a/Queue/PrintItemWrapper.cs b/Queue/PrintItemWrapper.cs index e5474e5db..6d5cac471 100644 --- a/Queue/PrintItemWrapper.cs +++ b/Queue/PrintItemWrapper.cs @@ -40,9 +40,9 @@ namespace MatterHackers.MatterControl.PrintQueue { public class PrintItemWrapper { - public RootedObjectEventHandler FileHasChanged = new RootedObjectEventHandler(); - public RootedObjectEventHandler SlicingDone = new RootedObjectEventHandler(); - public RootedObjectEventHandler SlicingOutputMessage = new RootedObjectEventHandler(); + public event EventHandler FileHasChanged; + public event EventHandler SlicingDone; + public event EventHandler SlicingOutputMessage; private static string fileNotFound = "File Not Found\n'{0}'".Localize(); private static string readyToPrint = "Ready to Print".Localize(); @@ -82,8 +82,6 @@ namespace MatterHackers.MatterControl.PrintQueue public bool CurrentlySlicing { get; set; } - public LibraryProvider SourceLibraryProvider { get; private set; } - public bool DoneSlicing { get @@ -121,7 +119,10 @@ namespace MatterHackers.MatterControl.PrintQueue OnSlicingOutputMessage(new StringEventArgs(message)); - SlicingDone.CallEvents(this, null); + if (SlicingDone != null) + { + SlicingDone(this, null); + } } } } @@ -198,6 +199,8 @@ namespace MatterHackers.MatterControl.PrintQueue public bool SlicingHadError { get { return slicingHadError; } } + public LibraryProvider SourceLibraryProvider { get; private set; } + public void Delete() { PrintItem.Delete(); @@ -274,7 +277,7 @@ namespace MatterHackers.MatterControl.PrintQueue if (FileHasChanged != null) { - FileHasChanged.CallEvents(this, null); + FileHasChanged(this, null); } } @@ -283,7 +286,7 @@ namespace MatterHackers.MatterControl.PrintQueue StringEventArgs message = e as StringEventArgs; if (SlicingOutputMessage != null) { - SlicingOutputMessage.CallEvents(this, message); + SlicingOutputMessage(this, message); } } } diff --git a/Queue/QueueRowItem.cs b/Queue/QueueRowItem.cs index d56a6c09d..c65302bc7 100644 --- a/Queue/QueueRowItem.cs +++ b/Queue/QueueRowItem.cs @@ -227,7 +227,7 @@ namespace MatterHackers.MatterControl.PrintQueue public override void OnClosed(EventArgs e) { - PrintItemWrapper.SlicingOutputMessage.UnregisterEvent(PrintItem_SlicingOutputMessage, ref unregisterEvents); + PrintItemWrapper.SlicingOutputMessage -= PrintItem_SlicingOutputMessage; if (unregisterEvents != null) { unregisterEvents(this, null); @@ -354,7 +354,7 @@ namespace MatterHackers.MatterControl.PrintQueue private void AddHandlers() { ActiveTheme.Instance.ThemeChanged.RegisterEvent(ThemeChanged, ref unregisterEvents); - PrintItemWrapper.SlicingOutputMessage.RegisterEvent(PrintItem_SlicingOutputMessage, ref unregisterEvents); + PrintItemWrapper.SlicingOutputMessage += PrintItem_SlicingOutputMessage; } private void ExportQueueItemWindow_Closed(object sender, EventArgs e)