Your command is registered with CommandFlags.Session,
(meaning it runs in the application context). Hence, you
must lock the Document whose Database you're accessing
before you try to do anything.
Generally, you only need to run your command in the
application context if there is some interaction with
multiple documents (such as closing or opening one,
or changing the active document).
If your command simply operates on the active document,
you don't need CommandFlags.Session.
--
http://www.caddzone.com
AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006
http://www.acadxtabs.com
wrote in message news:5067115@discussion.autodesk.com...
Hi all,
I have this code that crash vith visual studio 2005 & Autocad 2006 :
public class DBMain
{
[CommandMethod("ad_link",CommandFlags.Session )]
public static void OpenDB()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
//...
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
try
{
BlockTable bt = (BlockTable)trans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
//BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt["*MODEL_SPACE"], OpenMode.ForRead );
try
{
int count = 0;
foreach (ObjectId id in btr)
{
count += 1;
}
ed.WriteMessage(count.ToString());
}
catch (SystemException ex)
{
}
}
catch(Autodesk.AutoCAD.Runtime.Exception ex )
{
}
finally
{
trans.Dispose();
}
}
}
in line :
foreach (ObjectId id in btr)
{"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}
Anyone can help me ?
Thanks in advance
Max