CamBam
News:
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 12, 2019, 19:29:49 pm


Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Code Snippet: C# using Worker Threads  (Read 1578 times)
EddyCurrent
CNC Jedi
*****
Offline Offline

Posts: 4078



View Profile
« 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:
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 » Logged

Made in England
dh42
Administrator
CNC Jedi
*****
Offline Offline

Posts: 5676



View Profile WWW
« 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
Logged
dave benson
CNC Jedi
*****
Offline Offline

Posts: 1171


View Profile
« 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
Logged
EddyCurrent
CNC Jedi
*****
Offline Offline

Posts: 4078



View Profile
« 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
Logged

Made in England
dh42
Administrator
CNC Jedi
*****
Offline Offline

Posts: 5676



View Profile WWW
« 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
Logged
dave benson
CNC Jedi
*****
Offline Offline

Posts: 1171


View Profile
« 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
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines

Valid XHTML 1.0! Valid CSS! Dilber MC Theme by HarzeM
Page created in 0.094 seconds with 19 queries.

Copyright © 2018 HexRay Ltd. | Sitemap