Code Red > .NET
Disposing of DocumentLock Object
(1/1)
kdub_nz:
It's my understanding that LockDocument method requires a matching call to Dispose on the DocumentLock Object.
Usually this is achieved by the use of a using statement :
--- Code - C#: ---using (DocumentLock _documentLock = doc.LockDocument()) { ;; code block.. )
The
--- Code: ---using(Transaction ... )
--- End code ---
is then started inside the code block.
Once the using statement for the DocumentLock ends the database is unlocked
similar to the
--- Code: ---using (Transaction ... )
--- End code ---
Does the
--- Code: ---doc.CloseAndDiscard();
--- End code ---
call negate the requirement to Dispose the DocumentLock Object. ??
Regards,
It's Alive!:
I don’t think DocumentLock actually locks the database, I think it just prevents execution contexts
It’s not an AcDbObject, so I’m pretty sure that letting the garbage collector handle it is safe, Dispose only calls its destructor
BTW, doc.CloseAndDiscard(); calls the COM version of Document.close with the false parameter.
Also an FYI "The document does not need to be locked to open an AcDbObject in AcDb::kForRead, nor to get system variables."
according to the docs
kdub_nz:
Thanks Daniel, that's something to chew on.
The code I was looking at was changing Layer names in a side doc.
It just had
--- Code - C#: ---Document doc = Application.DocumentManager.Open(dwgPath);;; etcdoc.LockDocument();try{ using (Transaction ... ... { ;;code block to find and change layer names tr.Commit(); } db.SaveAs(dwgPath, DwgVersion.Current); doc.CloseAndDiscard();}catch( ;;; etc}
It was being called from a Form, iterating over a list of selected drawings.
. . . a little buzzer went off in my brain when I saw the LockDocument() was not disposed.
Stay well !
Navigation
[0] Message Index
Go to full version