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!?
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.
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()
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?