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.

Public Class WorkUnit
This language is not supported or no code example is available.
public class WorkUnit
This language is not supported or no code example is available.
Name Description
Public property ExecutionTime Gets the (last) work execution time in milliseconds.
Public property Log Gets the list of errors and warnings generated during execution.
Public property Status Gets the WorkUnit status.
Top
Methods
 
Name Description
Internal protected (Protected Friend) method Cancelled(BackgroundWorker, DoWorkEventArgs) Checks if the user has requested cancellation of the background operation.
Internal protected (Protected Friend) method DoWork(BackgroundWorker, DoWorkEventArgs) Does the actual work allowing progress bar update and cancellation.
Public method DoWork() Executes the work synchronously.
Public method Equals(Object) Determines whether the specified object is equal to the current object. (inherited from Object).
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (inherited from Object).
Public method GetHashCode Serves as the default hash function. (inherited from Object).
Public method GetType Gets the Type of the current instance. (inherited from Object).
Protected method MemberwiseClone Creates a shallow copy of the current Object. (inherited from Object).
Internal protected (Protected Friend) method ResetProgressParallel(int) Resets the progress bar for a parallel loop.
Internal protected (Protected Friend) method StartContinuousAnimation(string, BackgroundWorker, params string[]) Start the animation for the continuous progress bar.
Internal protected (Protected Friend) method StopContinuousAnimation(BackgroundWorker) Stop the animation for the continuous progress bar.
Public method ToString Returns a string that represents the current object. (inherited from Object).
Internal protected (Protected Friend) method UpdateProgress(double, double, string, BackgroundWorker, bool, params string[]) Updates the progress bar value.
Internal protected (Protected Friend) method UpdateProgress(int, int, string, BackgroundWorker, bool, params string[]) Updates the progress bar value.
Internal protected (Protected Friend) method UpdateProgressAndCheckCancelled(int, int, string, BackgroundWorker, DoWorkEventArgs, bool, params string[]) Updates the progress bar value and checks if the worker thread has been canceled.
Internal protected (Protected Friend) method 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.
Internal protected (Protected Friend) method UpdateProgressParallel(double, string, BackgroundWorker, params string[]) Updates the progress bar value for a parallel loop.
Internal protected (Protected Friend) method UpdateProgressTo100(string, BackgroundWorker, params string[]) Set the progress bar to 100% and forces screen update.
Internal protected (Protected Friend) method WorkCancelled(Environment) Called when the work is cancelled.
Internal protected (Protected Friend) method WorkCompleted(Environment) Called when the work has completed. In the case you are modeling something, the environment parameter allows you to easily add the model to the entities collection.
Internal protected (Protected Friend) method WorkFailed(Environment) Called when the work has failed.
Top
Events
 
Name Description
Public event ProgressChanged Occurs when the DoWork() is called synchronously and the progress has changed.
Top
Fields
 
Name Description
Internal protected (Protected Friend) field log
Top
Example
 
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
         Environment._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
          Environment._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.

.NET Framework

Supported in: 4.8

In this article

Definition