Author Topic: Lock and unlock a document  (Read 2298 times)

0 Members and 1 Guest are viewing this topic.

latour_g

  • Newt
  • Posts: 184
Lock and unlock a document
« 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.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Lock and unlock a document
« Reply #1 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.
« Last Edit: August 26, 2015, 12:24:08 PM by Andrey Bushman »

latour_g

  • Newt
  • Posts: 184
Re: Lock and unlock a document
« Reply #2 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;
                ....
        }
}


« Last Edit: August 26, 2015, 01:34:23 PM by latour_g »

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Lock and unlock a document
« Reply #3 on: August 26, 2015, 01:28:46 PM »
This is bad way. Why don't you need to do at in the finally?

latour_g

  • Newt
  • Posts: 184
Re: Lock and unlock a document
« Reply #4 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 ?

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Lock and unlock a document
« Reply #5 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.



latour_g

  • Newt
  • Posts: 184
Re: Lock and unlock a document
« Reply #6 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 !

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Lock and unlock a document
« Reply #7 on: August 26, 2015, 01:58:20 PM »
No problem. Welcome. :)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Lock and unlock a document
« Reply #8 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)
Speaking English as a French Frog

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Lock and unlock a document
« Reply #9 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
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013