Author Topic: Code Snippet: C# using Worker Threads  (Read 2427 times)

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 4468
    • View Profile
Code Snippet: C# using Worker Threads
« on: January 12, 2019, 21:16:46 pm »
I think I've got this sorted out now.

This example code shows how to use;

1. a worker thread.
2. using the "CamBam is Thinking" with Cancel option message.
3. getting a user input value
4. adding UNDO

For an example of this in an actual plugin please get the source code for "NonPlanarSilhouette" here;

http://www.cambam.co.uk/forum/index.php?topic=7799.msg62520#msg62520

Code: [Select]
using System;
using CamBam.CAD;
using CamBam.UI;
using CamBam;
using CamBam.Util;
using System.Threading;

namespace Example
{
    public class UsingThreads
    {
        protected static Thread _WorkerThread;
        protected static ICADView _WorkerView;
        private static double SomeValue;

        // this is the method called by the plugin
        public static void CalledFromMainProgram(ICADView view)
        {
            if (PolylineUtils.ConfirmSelected(CamBamUI.MainUI.ActiveView))
            {
                // just an example of getting a user input value
                object obj = ThisApplication.PromptForValue("Enter Value ", "Description of Value. ", typeof(double));
                if (obj != null)
                {
                     SomeValue = (double)obj;
                    _RunWorkerProcess(CamBamUI.MainUI.ActiveView, _WorkerProcessToRun);
                }
            }
            return;
        }


        protected static void _RunWorkerProcess(ICADView view, ThreadStart worker)
        {
            if (_WorkerThread == null)
            {
                _WorkerView = view;

                 CamBamUI.MainUI.ActiveView.StartThinking(true);

                _WorkerThread = new Thread(worker);
                ThisApplication.AddWorkerThread(_WorkerThread);
                ThisApplication.StartWorkerThreads();
            }
            return;
        }


        protected static void _WorkerProcessToRun()
        {
            try
            {
                // value entered by the user, if we need it.
                double step = SomeValue;

                // if Undo is required add these.
                _WorkerView.SuspendRefresh();
                CamBamUI.MainUI.UndoBuffer.AddUndoPoint(TextTranslation.Resource("NameOfProcess"));
                CamBamUI.MainUI.ActiveView.CADFile.EnsureActiveLayer(with_undo: true);
                CamBamUI.MainUI.UndoBuffer.Add(_WorkerView.CADFile.ActiveLayer.Entities);
              
            }
            catch (Exception ex)
            {
                ThisApplication.HandleException(ex);
            }
            finally
            {
                _WorkerProcessFinally();
            }
            return;
        }


        protected static void _WorkerProcessFinally()
        {
            CamBamUI.MainUI.ActiveView.StopThinking();
            if (_WorkerThread != null)
            {
                ThisApplication.RemoveWorkerThread(_WorkerThread);
                _WorkerThread = null;
            }
            return;
        }
    }
}
« Last Edit: January 13, 2019, 11:48:43 am by EddyCurrent »
Made in England

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6141
    • View Profile
    • Cambam V1.0 French Doc
Re: Code Snippet: C# using Worker Threads
« Reply #1 on: January 13, 2019, 01:19:34 am »
Hello

Added to the menu

A worker Thread ? ... it is a way to run multiple tasks on multiple cores of the CPU ?

++
David

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1345
    • View Profile
Re: Code Snippet: C# using Worker Threads
« Reply #2 on: January 13, 2019, 02:18:23 am »
Good (Solution \Project) Ed
One thing to mention,(for thoses just starting out)  is that when you load up the project
You'll maybe greated with a sea of red, and the CamBam .dll's will have a warning sign next to them.
Your CamBam Reference files may be in a different folder than the projects.
Delete the two files and right click the References and add reference, and point VS at your
CamBam .dll files.
Dave

Offline EddyCurrent

  • CNC Jedi
  • *****
  • Posts: 4468
    • View Profile
Re: Code Snippet: C# using Worker Threads
« Reply #3 on: January 13, 2019, 09:22:55 am »
Hello

Added to the menu

A worker Thread ? ... it is a way to run multiple tasks on multiple cores of the CPU ?

++
David

David,

That is how I understand it.

From Microsoft;

"System.Threading Namespace
The System.Threading namespace provides classes and interfaces that enable multithreaded programming."

More info here; https://docs.microsoft.com/en-us/dotnet/api/system.threading.thread?view=netframework-4.7.2
Made in England

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 6141
    • View Profile
    • Cambam V1.0 French Doc
Re: Code Snippet: C# using Worker Threads
« Reply #4 on: January 14, 2019, 00:10:58 am »
Hello

Something that I wonder about multiple thread usage is: can we use a same set of data for 2 different threads ?

Ex: let's say we want to calculate "toolpaths" based on a 3D shape ; we can use multiple thread to do calculation on different areas of the shape at the same time (to speed up things), but in this case the 3D shape is shared for all thread ; it's is possible ? or each thread must have it's own data set ? ...

++
David

Offline dave benson

  • CNC Jedi
  • *****
  • Posts: 1345
    • View Profile
Re: Code Snippet: C# using Worker Threads
« Reply #5 on: January 14, 2019, 01:09:57 am »
HI David

https://stackoverflow.com/questions/38962563/how-can-i-run-4-threads-each-on-a-different-coreparallelism
Yes in short you can, However on a video I saw (where they tested the Windows Scheduler) they found that
it was very good at doing this it'self and only in some circumstances was there any benefit in doing manually.

Dave