TheSwamp

Code Red => .NET => Topic started by: latour_g on August 26, 2015, 12:06:16 PM

Title: Lock and unlock a document
Post by: latour_g on August 26, 2015, 12:06:16 PM
Hi,

I would like to know if using

Code: [Select]
using (doc.LockDocument())
{
.....
}

is better than using :

Code: [Select]
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
DocumentLock loc = doc.LockDocument();
....
loc.Dispose();

Right now, I'm using doc.LockDocument() at a lot of place in my code.  I'm wondering if I choose change them all for the other method because user sometime get an error message when trying to save (Command still active).  I have checked everywhere in my code, there is always a loc.Dispose() but I might have miss something.

Thank youbefore printing this email.
Title: Re: Lock and unlock a document
Post by: Andrey Bushman on August 26, 2015, 12:17:04 PM
If you use the using block, then document will be unlocked even if an unhandled exception happens. If you use
Code: [Select]
DocumentLock loc = doc.LockDocument();then you are to use 'try\catch\finally' block and unlock document in the 'finally' block.
Title: Re: Lock and unlock a document
Post by: latour_g on August 26, 2015, 01:20:45 PM
Thanks for your answer.

Suppose I have this :

Code: [Select]
try { db.LoadLineTypeFile(clType, cLinFile); }
catch { }

Can I unlock the document in the catch since there is nothing ?

And one last question if I may.  If I want to replace this :

Code: [Select]
DocumentLock loc = doc.LockDocument();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBObjectCollection objs = new DBObjectCollection();
BlockReference br = tr.GetObject(blkId, OpenMode.ForWrite) as BlockReference;
.....

with "using document lock"

should I declare using (doc.LockDocument()) inside or outside using (Transaction tr = db.TransactionManager.StartTransaction()) ?

Code: [Select]
using (doc.LockDocument())
{
        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
                DBObjectCollection objs = new DBObjectCollection();
                BlockReference br = tr.GetObject(blkId, OpenMode.ForWrite) as BlockReference;
                ....
        }
}

or

Code: [Select]
using (Transaction tr = db.TransactionManager.StartTransaction())
{
        using (doc.LockDocument())
        {
                DBObjectCollection objs = new DBObjectCollection();
                BlockReference br = tr.GetObject(blkId, OpenMode.ForWrite) as BlockReference;
                ....
        }
}


Title: Re: Lock and unlock a document
Post by: Andrey Bushman on August 26, 2015, 01:28:46 PM
This is bad way. Why don't you need to do at in the finally?
Title: Re: Lock and unlock a document
Post by: latour_g on August 26, 2015, 01:33:19 PM
I thought "finally" was useless if there was nothing to be done in the catch.

I have edited my previous post, can you take a look at it please ?
Title: Re: Lock and unlock a document
Post by: Andrey Bushman on August 26, 2015, 01:40:45 PM
Quote
should I declare using (doc.LockDocument()) inside or outside using (Transaction tr = db.TransactionManager.StartTransaction()) ?
I think both variants are correct.
Quote
I thought "finally" was useless if there was nothing to be done in the catch.
Content of the finally block will be called always. Content of the catch block will be called only if exception occurs. Read about the finally in the C# specification, please.


Title: Re: Lock and unlock a document
Post by: latour_g on August 26, 2015, 01:45:38 PM
Yes you are right.  My bad, I was confused when I talked about try/catch !

Thanks for your answer !
Title: Re: Lock and unlock a document
Post by: Andrey Bushman on August 26, 2015, 01:58:20 PM
No problem. Welcome. :)
Title: Re: Lock and unlock a document
Post by: gile on August 26, 2015, 02:09:14 PM
Hi,

By my side I use to write :

Code - C#: [Select]
  1. using (doc.LockDocument())
  2. using (var tr = db.TransactionManager.StartTransaction())
  3. {
  4.     // ...
  5. } // <- the transaction is disposed, then the document is unlocked (disposed)
Title: Re: Lock and unlock a document
Post by: Keith Brown on August 26, 2015, 04:19:04 PM
Hi,

By my side I use to write :

Code - C#: [Select]
  1. using (doc.LockDocument())
  2. using (var tr = db.TransactionManager.StartTransaction())
  3. {
  4.     // ...
  5. } // <- the transaction is disposed, then the document is unlocked (disposed)


+1