You have answered twice to my question.
The first answer:
"Yes I lock the document (variable named ThisDrawing) with ThisDrawing.LockDocument()
Then at the end of the function I use this:
ThisDrawing.LockDocument(DocumentLockMode.NotLocked, string.Empty, string.Empty, false);"
describes a wrong method.
The second answer:
" Re: Document "xxx.dwg" has a command in progress.
Document ThisDrawing = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocum ent;
DocumentLock dLock = ThisDrawing.LockDocument();
// modify the database here
dLock.Dispose();"
is correct.
In general, you should lock the document any way you want, but to unlock it, you just dispose the object you got when you locked the doc. I don't know what "NotLocked" is for, but it sure does not unlock the document.
This locks the doc (as you already know):
DocumentLock dLock = ThisDrawing.LockDocument(DocumentLockMode....., ...., ...., ....);
then you do your work...
...
and lastly you unlock the doc to allow for other commands to manipulate it:
dLock.Dispose();