.NET

Reply
Active Contributor
33 Posts
1 Kudo
Registered: ‎06-23-2011
Post 1 of 6

Parallel Loop

261 Views, 5 Replies
12-06-2012 12:55 PM

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!?

 

ADN Support Specialist
433 Posts
67 Kudos
Registered: ‎05-22-2006
Post 2 of 6

Re: Parallel Loop

12-06-2012 02:49 PM in reply to: MGOMKD

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
*Expert Elite*
2,140 Posts
253 Kudos
Registered: ‎04-29-2006
Post 3 of 6

Re : Parallel Loop

12-07-2012 03:47 AM in reply to: MGOMKD

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
Active Contributor
33 Posts
1 Kudo
Registered: ‎06-23-2011
Post 4 of 6

Re : Parallel Loop

12-10-2012 05:18 AM in reply to: MGOMKD

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?

*Expert Elite*
2,140 Posts
253 Kudos
Registered: ‎04-29-2006
Post 5 of 6

Re : Parallel Loop

12-10-2012 10:40 AM in reply to: MGOMKD

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
Active Contributor
33 Posts
1 Kudo
Registered: ‎06-23-2011
Post 6 of 6

Re : Parallel Loop

12-10-2012 12:13 PM in reply to: MGOMKD

Salut Gilles!

 

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

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post