Parallel Loop

Parallel Loop

MGOMKD
Enthusiast Enthusiast
1,063 Views
5 Replies
Message 1 of 6

Parallel Loop

MGOMKD
Enthusiast
Enthusiast

Hi All,

 

I would like to change my source code from "For Each" to "Parallel.Foreach" as to increase processing by using Threads!

Each time, I've failed in changing this code and my net searching were unsucceeded!

 

Here's my source with "For Each" loop:

Using acTrans = acCurDb.TransactionManager.StartTransaction()
    acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
    acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForRead)

             For Each acObjId As ObjectId In acBlkTblRec
                    Dim MyEnt As Entity = acTrans.GetObject(acObjId, OpenMode.ForRead)
                    If MyEnt.Layer = LayerVal Then

                        'Nombre d'objets dans le calque séléctionné'
                        iMax += 1

                        'Nombre d'objets à traiter dans le calque séléctionné'
                        Select Case acObjId.ObjectClass.DxfName
                            Case "ARC", "CIRCLE", "LINE", "LWPOLYLINE", "POINT", "REGION"
                                iUtil += 1
                        End Select

                    End If
                Next acObjId
End Using

I know that "Parallel.Foreach" deal with "IEnumerable Class" but i'm really lost!

 

Any idea!?

 

0 Likes
1,064 Views
5 Replies
Replies (5)
Message 2 of 6

StephenPreston
Alumni
Alumni

Sorry for the bad news, but the AutoCAD API isn't thread safe. You can get away with multithreaded apps if you only access the API from the main thread, but that's not what you're doing in your example.

 

 

Cheers,

Stephen Preston
Autodesk Developer Network
Message 3 of 6

_gile
Consultant
Consultant

Hi,

 

You can try using Parallel Linq, but it would only work with ObjectId collections not while opening them to get entities, so the benefit won't be so large.

 

Anyway, here's an example from the code you posted:

C#

            string[] classes = {"ARC", "CIRCLE", "LINE", "LWPOLYLINE", "POINT", "REGION"};
            ObjectId[] ids;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr =
                    (BlockTableRecord)tr.GetObject(
                        SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
                ids = btr
                    .Cast<ObjectId>()
                    .Where(id => ((Entity)tr.GetObject(id, OpenMode.ForRead)).Layer == LayerVal)
                    .ToArray();
                tr.Commit();
            }
            iMax = ids.Length;
            iUtil = ids
                .AsParallel()
                .Where(id => classes.Contains(id.ObjectClass.DxfName))
                .Count();

 

VB

            Dim classes As String() = {"ARC", "CIRCLE", "LINE", "LWPOLYLINE", "POINT", "REGION"}
            Dim ids As ObjectId()
            Using tr As Transaction = db.TransactionManager.StartTransaction()
                Dim btr As BlockTableRecord = _
                    DirectCast(tr.GetObject( _
                            SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead), BlockTableRecord)
                ids = btr _
                    .Cast(Of ObjectId)() _
                    .Where(Function(id) DirectCast(tr.GetObject(id, OpenMode.ForRead), Entity).Layer = LayerVal) _
                    .ToArray()
                tr.Commit()
            End Using

            iMax = ids.Length
            iUtil = ids _
                .AsParallel() _
                .Where(Function(id) classes.Contains(id.ObjectClass.DxfName)) _
                .Count()

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 6

MGOMKD
Enthusiast
Enthusiast

Sorry to learn it doesn't work! I hope it will with another API and Framework version!?

 

Thanks a lot Gilles, I will work on your Source Code soon.

 

PS: Are you french Gilles?

0 Likes
Message 5 of 6

_gile
Consultant
Consultant

MGO_MKD a écrit :
Thanks a lot Gilles, I will work on your Source Code soon.

 

PS: Are you french Gilles?


You're welcome.

Yes, I'm French.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 6

MGOMKD
Enthusiast
Enthusiast

Salut Gilles!

 

Je suis également français, ça fait plaisir de ne pas se sentir seul!

0 Likes