Author Topic: Document lock  (Read 2215 times)

0 Members and 1 Guest are viewing this topic.

latour_g

  • Newt
  • Posts: 184
Document lock
« on: May 29, 2014, 02:43:10 PM »
#1 :
Code: [Select]
using (doc.LockDocument())
{
     .... some code ...
}

#2 :
Code: [Select]
DocumentLock loc = doc.LockDocument();
... some code ...
loc.Dispose()

Does #1 is more reliable than #2 since there is no need to dispose of the lock ?

Also, if I use #2 and something goes wrong in the code and the loc.Dispose() is not being done, is there a way in Autocad to know wich command is stuck ?
And is there a command that would cancel any lock that would not have been dispose ?

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Document lock
« Reply #1 on: May 29, 2014, 02:52:56 PM »
Does #1 is more reliable than #2 since there is no need to dispose of the lock ?
Yes. It also guarantees the Dispose() call in the case of unexpected exception.

And is there a command that would cancel any lock that would not have been dispose ?
The Dispose() will called every time you leave the "using" block.

Also, if I use #2 and something goes wrong in the code and the loc.Dispose() is not being done, is there a way in Autocad to know wich command is stuck ?
What hinders you to use the first variant and not to create the problems to yourself?

Also, if I use #2 and something goes wrong in the code and the loc.Dispose() is not being done, is there a way in Autocad to know wich command is stuck ?
If you are in the same scope, you can check the IsDisposed property.

latour_g

  • Newt
  • Posts: 184
Re: Document lock
« Reply #2 on: May 29, 2014, 03:01:59 PM »
I wasn't aware of the second variant so I used the first one everywhere in my code when needed.  But knowing that now I will change them all !


Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Document lock
« Reply #3 on: May 29, 2014, 03:05:38 PM »
First variant (with the "using" block) is the most reliable and right.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Document lock
« Reply #4 on: May 29, 2014, 03:16:36 PM »
First variant (with the "using" block) is the most reliable and right.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: Document lock
« Reply #5 on: May 29, 2014, 03:16:57 PM »
Just a FYI, use a reflection tool to look at the compiled IL in your assembly and you'll see that a using statement is just syntactic sugar. 
Code - C#: [Select]
  1. using (Resource)
  2. {
  3.    code...
  4. }
Is equivalent to
Code - C#: [Select]
  1. var Resource = blah blah;
  2. try
  3. {
  4.    code...
  5. }
  6. finally
  7. {
  8.    Resource.dispose();
  9. }

I have created a transaction class on this concept which automatically commits unless explicitly aborted

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Document lock
« Reply #6 on: May 30, 2014, 07:55:45 AM »
Just a FYI, use a reflection tool to look at the compiled IL in your assembly and you'll see that a using statement is just syntactic sugar.


MSDN help basically says the same thing.  There is no difference between using statement and a try/catch/finally statement.  The biggest advantage to the using statement is that you do not have to remember to dispose the object.  It will do it for you.


http://msdn.microsoft.com/en-us/library/yh598w02.aspx


It is always a good idea to read the instructions and in the case of C#, MSDN Help does have alot of good advice.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013