Author Topic: Disposing of DocumentLock Object  (Read 190 times)

0 Members and 1 Guest are viewing this topic.

kdub

  • Mesozoic keyThumper
  • SuperMod
  • Swamp Rat
  • Posts: 1463
  • class keyThumper<T>:ILazy<T>
Disposing of DocumentLock Object
« on: July 05, 2022, 10:28:59 PM »
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#: [Select]
  1. using (DocumentLock _documentLock = doc.LockDocument())
  2.     {
  3.     ;; code block..
  4.     )

The
Code: [Select]
using(Transaction ... ) is then started inside the code block.

Once the using statement for the DocumentLock ends the database is unlocked

similar to the 
Code: [Select]
using (Transaction ... )
 

Does the
Code: [Select]
doc.CloseAndDiscard();    call negate the requirement to Dispose the  DocumentLock Object. ??

Regards,
called Kerry in my other life

Sometimes the question is more important than the answer.

I don't really work crazy hours . . I just live at UTC + 12.00
#ridesober

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 7320
  • AKA Daniel
Re: Disposing of DocumentLock Object
« Reply #1 on: July 06, 2022, 06:19:38 AM »
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
« Last Edit: July 06, 2022, 06:31:12 AM by It's Alive! »
Retired

kdub

  • Mesozoic keyThumper
  • SuperMod
  • Swamp Rat
  • Posts: 1463
  • class keyThumper<T>:ILazy<T>
Re: Disposing of DocumentLock Object
« Reply #2 on: July 06, 2022, 05:40:21 PM »
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#: [Select]
  1. Document doc = Application.DocumentManager.Open(dwgPath);
  2. ;; etc
  3. doc.LockDocument();
  4. try
  5. {
  6.     using (Transaction ... ...
  7.     {
  8.          ;;code block to find and change layer names
  9.          tr.Commit();
  10.      }
  11.      db.SaveAs(dwgPath, DwgVersion.Current);
  12.      doc.CloseAndDiscard();
  13. }
  14. catch(
  15. ;;; etc
  16. }
  17.  
  18.  

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 !



called Kerry in my other life

Sometimes the question is more important than the answer.

I don't really work crazy hours . . I just live at UTC + 12.00
#ridesober