In this post, I use some code to show what happen when AutoCAD is closed by user (via clicking "Exit" menu, click "x" on AutoCAD main window, or issue command "quit").
In order to track what happen during the AutoCAD closing process, various event handlers are attached to drawings that were open when AutoCAD starts closing, drawing databases and AutoCAD application itself. Because AutoCAD is closing, AutoCAD test window cannot be used to show message regarding the closing process, I created a TextLogger class that holds all closing messages captured in various event handlers and save them to a text file at the end of Terminate() call.
Here is the code of class TextLogger:
Here is the code that runs an AutoCAD:
I used 2 forms, which are shown in AutoCAD as modeless dialog box. Both forms are very simple with only one button "Close" on the forms. Clicking the button triggers this.Close() method.
In order to make a point in Terminate() process, I let one form to be closed normally (i.e. when call Form.Close() on a modeless form, the form is disposed). I let the other form change to invisible when Form.Close() is called by handling Form_Closing event, like this:
Build the code into an assembly (dll file) with VS. Now it is ready to run the code and see what happen during AutoCAD closing process. I do these steps:
1. Start AutoCAD;
2. Netload the DLL file;
3. Open a few drawing. In my case, I open 3 saved drawing: drawing1, drawing 2 and drawing 3 from a folder;
4. Execute command "ShowForms" to bring up the 2 modeless forms, then cloce them by clicking their "Close" button;
5. Close AutoCAD without closing drawing first by going to big "A" button->Exit AutoCAD, or simply click "x" on main AutoCAD window;
6. Open the log file ("E:\Temp\AcadClosingEvents.txt") in NotePad to see what have been logged.
Here is the content of the log file:
Document C:\Users\norm\Documents\Drawing3.dwg closing begins Document C:\Users\norm\Documents\Drawing3.dwg closing begins Document C:\Users\norm\Documents\Drawing3.dwg is about to be destroyed Database in document C:\Users\norm\Documents\Drawing3.dwg is about to be destroyed Document C:\Users\norm\Documents\Drawing3.dwg has been destroyed Document C:\Users\norm\Documents\Drawing2.dwg closing begins Document C:\Users\norm\Documents\Drawing2.dwg closing begins Document C:\Users\norm\Documents\Drawing2.dwg is about to be destroyed Database in document C:\Users\norm\Documents\Drawing2.dwg is about to be destroyed Document C:\Users\norm\Documents\Drawing2.dwg has been destroyed Document C:\Users\norm\Documents\Drawing1.dwg closing begins Document C:\Users\norm\Documents\Drawing1.dwg closing begins Document C:\Users\norm\Documents\Drawing1.dwg is about to be destroyed Database in document C:\Users\norm\Documents\Drawing1.dwg is about to be destroyed Document C:\Users\norm\Documents\Drawing1.dwg has been destroyed Quiting Autodesk begins Autodesk is about to quit Terminate() is called Document count is 0 Form1 is disposed, but its reference is still alive Form2 has not been disposed Terminate() call is completed
Form the messages logged in various event handlers we can see:
1. After AutoCAD receives "quit" command, if there is open drawing, AutoCAD will attempt to close all open drawings, which may trigger prompting message asking user to save changes. The quiting process can be cancelled if user click "Cancel" button in the message box.
2. After all open drawing documents are closed, AutoCAD starts quiting, it is only then the IExtensionApplication.Terminate() is called. That is, one cannot do any document/database related clean-up task in Terminate(), because all document is gone. However, DocumentCollection object is still reachable, but it does not contain document any more.
3. Custom object declared at command class level may or may not exist when Terminate() is executed. In my example, since the 2 forms is called in a static command method, thus, they are instantiated at AutoCAD session level, not per document level. Since they are instantiated in static method, they also has to be declared as "static". About the difference of "static" command method and "non-static" command method, one of my previous post discussed it in more details here.
4. As I commented in the code, what clean-up tasks we need to do in the Terminate() method depends on what our custom AutoCAD Addin does, what external (to AutoCAD) resources the code uses and how they are used. If your code have to do some clean-up in Terminate(), such as opened database connection, opened data file...You may want to look back to the code to answer the question: why your code holds those resource until the end of AutoCAD session? In most cases, it might not be a good practice. So, in real practice, there aren't many cases one has to stuff Terminate() method with a lot of code, if things are done correctly.