WorkUnit Class
Base class for background working support. Overriding this class you can build a model or do your CPU intensive calculations on a separate thread keeping your application UI responsive.
Depending on the nature of the work you may need to override both the DoWork and WorkCompleted methods or only the DoWork one and provide a WorkCompletedEventHandler to be notified when the work has completed.
Name | Description | |
---|---|---|
ExecutionTime | Gets or sets the (last) work execution time in milliseconds. | |
Log | Gets the list of errors and warnings generated during execution. | |
Status | Gets the WorkUnit status. |
Name | Description | |
---|---|---|
AppendToLog(string) | Appends a message to the log. | |
Cancelled(BackgroundWorker, DoWorkEventArgs) | Checks if the user has requested cancellation of the background operation. | |
DoWork(BackgroundWorker, DoWorkEventArgs) | Does the actual work allowing progress bar update and cancellation. | |
DoWork() | Executes the work synchronously. | |
Equals(Object) | (inherited from Object). | |
Finalize | (inherited from Object). | |
GetHashCode | (inherited from Object). | |
GetType | (inherited from Object). | |
MemberwiseClone | (inherited from Object). | |
ResetProgress() | Resets the progress. | |
ResetProgressParallel(int) | Resets the progress bar for a parallel loop. | |
StartContinuousAnimation(string, BackgroundWorker, params string[]) | Start the animation for the continuous progress bar. | |
StopContinuousAnimation(BackgroundWorker) | Stop the animation for the continuous progress bar. | |
ToString | (inherited from Object). | |
UpdateProgress(double, double, string, BackgroundWorker, bool, params string[]) | Updates the progress bar value. | |
UpdateProgress(int, int, string, BackgroundWorker, bool, params string[]) | Updates the progress bar value. | |
UpdateProgressAndCheckCancelled(int, int, string, BackgroundWorker, DoWorkEventArgs, bool, params string[]) | Updates the progress bar value and checks if the worker thread has been canceled. | |
UpdateProgressAndCheckCancelledParallel(int, string, BackgroundWorker, DoWorkEventArgs, params string[]) | Updates the progress bar value and checks if the worker thread has been canceled, for a parallel loop. | |
UpdateProgressParallel(double, string, BackgroundWorker, params string[]) | Updates the progress bar value for a parallel loop. | |
UpdateProgressTo100(string, BackgroundWorker, params string[]) | Set the progress bar to 100% and forces screen update. | |
WorkCancelled(object) | Called when the work is cancelled. | |
WorkCompleted(object) | Called when the work has completed. In the case you are modeling something, the sender parameter allows you to easily add the content to the entities collection. | |
WorkFailed(object) | Called when the work has failed. |
Name | Description | |
---|---|---|
ProgressChanged | Occurs when the DoWork() is called synchronously and the progress has changed. |
Name | Description | |
---|---|---|
ProgressChangedEventArgs | Provides data for the WorkUnit.ProgressChanged event. |
Name | Description | |
---|---|---|
ProgressChangedEventHandler | Represents the method that will handle the WorkUnit.ProgressChanged event. | |
WorkCancelledEventHandler | Represents the method that will handle the WorkUnit.WorkCancelled event. | |
WorkCompletedEventHandler | Represents the method that will handle the WorkUnit.WorkCompleted event. | |
WorkFailedEventHandler | Represents the method that will handle the WorkUnit.WorkFailed event. |
Name | Description | |
---|---|---|
HighestNumberOfTicks | Max number of progress bar updates. This means that the screen is repainted a max of 32 times even if it gets 100 progress updates. | |
log |
The following sample demonstrates the WorkUnit class usage for building a solid model.
public void Lego(Viewport viewport) { BuildLego bl = new BuildLego(); viewport.StartWork(bl); } private class BuildLego : WorkUnit { private Solid[] model; protected override void DoWork(System.ComponentModel.BackgroundWorker worker, System.ComponentModel.DoWorkEventArgs doWorkEventArgs) { // dimensions double width = 95.75; double depth = 47.75; double height = 3.25; // rows ans columns number int nRows = 6; int nColumns = 12; // top cylinders size and position double cylRadius = 2.55; double distFromBorder = 1; double cylHeight = 1.6; Point2D startPos = new Point2D(3.55, 3.55); // creates the plate model = new Solid[]{Solid.CreateBox(width, depth, height)}; // adds cylinders to the upper side double xOffset, yOffset; if (nColumns <= 1) xOffset = 0; else xOffset = (width - 2 * distFromBorder - 2 * cylRadius) / (nColumns - 1); if (nRows <= 1) yOffset = 0; else yOffset = (depth - 2 * distFromBorder - 2 * cylRadius) / (nRows - 1); Circle circle = new Circle(startPos.X, startPos.Y, height, cylRadius); CompositeCurve curve = new CompositeCurve(circle); int currentValue = 0; for (int i = 0; i < nRows; i++) { Solid solid = model[0]; for (int j = 0; j < nColumns; j++) { model = solid.ExtrudeAdd(curve, 0.2, new Vector3D(0, 0, cylHeight), true); if (model != null) { solid = model[0]; curve.Translate(xOffset, 0, 0); // updates progress using build in progress bar UpdateProgress(++currentValue, nRows * nColumns, "Adding top cylinders...", worker); // checks for cancellation using built in progress bar cancel button if (Canceled(worker, doWorkEventArgs)) return; } } curve.Translate(-nColumns * xOffset, yOffset, 0); } } protected override void WorkCompleted(Viewport viewport) { // adds the result to viewport entities collection Workspace._entities.AddRange(model, 0, Color.Green); viewport.ZoomFit(); // prints the execution time Console.WriteLine("Execution time: " + ExecutionTime + " ms"); } }
This language is not supported or no code example is available.
Public Sub Lego(viewport As Viewport) Dim bl As New BuildLego() viewport.StartWork(bl) End Sub Private Class BuildLego Inherits WorkUnit Private model As Solid() Protected Overrides Sub DoWork(worker As System.ComponentModel.BackgroundWorker, doWorkEventArgs As System.ComponentModel.DoWorkEventArgs) ' dimensions Dim width As Double = 95.75 Dim depth As Double = 47.75 Dim height As Double = 3.25 ' rows ans columns number Dim nRows As Integer = 6 Dim nColumns As Integer = 12 ' top cylinders size and position Dim cylRadius As Double = 2.55 Dim distFromBorder As Double = 1 Dim cylHeight As Double = 1.6 Dim startPos As New Point2D(3.55, 3.55) ' creates the plate model = New Solid() {Solid.CreateBox(width, depth, height)} ' adds cylinders to the upper side Dim xOffset As Double, yOffset As Double If nColumns <= 1 Then xOffset = 0 Else xOffset = (width - 2 * distFromBorder - 2 * cylRadius) / (nColumns - 1) End If If nRows <= 1 Then yOffset = 0 Else yOffset = (depth - 2 * distFromBorder - 2 * cylRadius) / (nRows - 1) End If Dim circle As New Circle(startPos.X, startPos.Y, height, cylRadius) Dim curve As New CompositeCurve(circle) Dim currentValue As Integer = 0 For i As Integer = 0 To nRows - 1 Dim theSolid As Solid = model(0) For j As Integer = 0 To nColumns - 1 model = theSolid.ExtrudeAdd(curve, 0.2, New Vector3D(0, 0, cylHeight), True) If model IsNot Nothing Then theSolid = model(0) curve.Translate(xOffset, 0, 0) ' updates progress using build in progress bar UpdateProgress(System.Threading.Interlocked.Increment(currentValue), nRows * nColumns, "Adding top cylinders...", worker) ' checks for cancellation using built in progress bar cancel button If Canceled(worker, doWorkEventArgs) Then Return End If End If Next curve.Translate(-nColumns * xOffset, yOffset, 0) Next End Sub Protected Overrides Sub WorkCompleted(viewport As Viewport) ' adds the result to viewport entities collection Workspace._entities.AddRange(model, 0, Color.Green) viewport.ZoomFit() ' prints the execution time Console.WriteLine("Execution time: " + ExecutionTime + " ms") End Sub End Class
This language is not supported or no code example is available.
devDept.WorkUnit