What is faster: Open a Transaction or InvokeMember.

What is faster: Open a Transaction or InvokeMember.

joantopo
Mentor Mentor
882 Views
3 Replies
Message 1 of 4

What is faster: Open a Transaction or InvokeMember.

joantopo
Mentor
Mentor

I have this in an extension method:

 

   object sample = idSample.GetObject(OpenMode.ForRead).AcadObject;
             Autodesk.Civil.DatabaseServices.SampleLineGroup slg = (Autodesk.Civil.DatabaseServices.SampleLineGroup)Autodesk.AutoCAD.DatabaseServices.DBObject.FromAcadObject((object)sample.GetType().InvokeMember("Parent", System.Reflection.BindingFlags.GetProperty,
               null, sample, null)).GetObject(OpenMode.ForRead);

 

But I could open a Transaction, red the Sample Line and do :  SampleLine.GroupId, so , what is the faster way?

Autocad C3D 2019 SP3, 2020 & 2021
Intel I9 9900K with frontal watercooler alphacool eisbaer 360 (original fans mounted in pull)- 3 fans Corsair 120 ML PRO in push.
MOBO Gygabyte Z390 Aorus Master- Corsair RGB Vengeance 64GB RAM (4x16) CL16
Nvidia Quadro RTX 4000
Samsung 970 EVO PLUS 1TB (unit C). Samsung 970 PRO 512GB (for data)
Power Supply: Corsair TX850M PLUS


Descubre mi programa VisorNET para Civil 3D:
https://apps.autodesk.com/CIV3D/es/Detail/Index?id=appstore.exchange.autodesk.com%3avisornet_windows32and64%3aes
0 Likes
Accepted solutions (2)
883 Views
3 Replies
Replies (3)
Message 2 of 4

augusto.goncalves
Alumni
Alumni
Accepted solution

The main difference between opening a transaction and opening the object directly is that you need to manually close them at the end, otherwise AutoCAD Civil 3D may have problems..

 

I would recommend using a transaction for safety, and with the using keyword

 

Now there are 2 types of transactions:

StartTransaction: full transaction that will control all objects, but is optimized for a big group of objects

StartOpenCloseTransaction: encapsulate the ObjectId.GetObject that you are using, also ensure the objects are disposed, and it's optimized for a small set of objects (per transaction)

 

Hope that clarifies.

Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
Message 3 of 4

joantopo
Mentor
Mentor

Thanks Augusto.

 

I have another question about this topic.

 

What happens if the process ends up before doing "trans.Commit();"?

 

For example, if we use "return;"

 

public static bool test1()
{
bool var1=false;

 Document doc =
              Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            Transaction tr =
              db.TransactionManager.StartTransaction();
            using (tr)
            {

                          if(number > 5)
                          {
                            return var1=true;
                           }

             tr.Commit();
            }

return var1;
}

 If the conditional is true, then it will do "return" before end up the process, and it doesn´t do "tr.Comit()".

 

I think if the transaction is only for read, there is no matter, but we have to avoid this scenario if the transaction is for writting.

 

 

 

 

Autocad C3D 2019 SP3, 2020 & 2021
Intel I9 9900K with frontal watercooler alphacool eisbaer 360 (original fans mounted in pull)- 3 fans Corsair 120 ML PRO in push.
MOBO Gygabyte Z390 Aorus Master- Corsair RGB Vengeance 64GB RAM (4x16) CL16
Nvidia Quadro RTX 4000
Samsung 970 EVO PLUS 1TB (unit C). Samsung 970 PRO 512GB (for data)
Power Supply: Corsair TX850M PLUS


Descubre mi programa VisorNET para Civil 3D:
https://apps.autodesk.com/CIV3D/es/Detail/Index?id=appstore.exchange.autodesk.com%3avisornet_windows32and64%3aes
0 Likes
Message 4 of 4

augusto.goncalves
Alumni
Alumni
Accepted solution

If no Transaction.Commit is reach, the AutoCAD will assume a Transaction.Abort, always. So if you change something, then you need to commit.

 

Now the engine is optimized for Commit, meaning if you Abort (or don't do anything), internally AutoCAD will rollback, which consumes more processing.

 

If you only read, then put a Commit just to be faster.

Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
0 Likes