When AutoCAD is in a zero document state, you have very limited access to AutoCAD’s functionality. One straightforward method of solving this problem is to prevent AutoCAD from entering the zero document state.
For this, handle the “DocumentCollection.DocumentToBeDestroyed” event, in this event handler; make a call to “ExecuteInApplicationContext” passing a callback. Add a new document, when AutoCAD calls the callback function.
static bool bBeginQuit = false;
[CommandMethod("ZeroDocState")]
public static void ZeroDocState()
{
Application.BeginQuit +=new EventHandler(Application_BeginQuit);
DocumentCollection docMg = Application.DocumentManager;
docMg.DocumentToBeDestroyed +=
new DocumentCollectionEventHandler(docMg_DocumentToBeDestroyed);
}
static void Application_BeginQuit(object sender, EventArgs e)
{
bBeginQuit = true;
}
static void docMg_DocumentToBeDestroyed(object sender,
DocumentCollectionEventArgs e)
{
DocumentCollection docman = (DocumentCollection)sender;
//1 - means, after closing the document AutoCAD will
//be in zero document state, so call ExecuteInApplicationContext
if (docman.Count == 1)
{
Application.DocumentManager.ExecuteInApplicationContext(
ApplicationContextAdd, null);
}
}
static void ApplicationContextAdd(object data)
{
try
{
if(!bBeginQuit)
Application.DocumentManager.Add("");
}
catch
{
}
}

Leave a Reply to Stephen PrestonCancel reply