You need to read up on IExtensionApplication, because you
are not understanding how it is used.
Your code is creating an instance of this class, and then is
calling the Initialize() method, both of which is a no-no.
When your assembly loads, AutoCAD creates an instance
of the class that implements IExtensionApplication, and then
calls its Initialize() method, so you should never create an
instance of that class, and should never call the Initialize()
or terminate methods.
That also means there is no point to the '_initiated' variable,
because no code in your assembly can read that variable
before it is set to true.
From your CommandMethods and LispFunctions, you can
rely on the fact that an instance of your PersistenceTest
class has already been created and its Initialize() method
called (by AutoCAD).
When you use IExtensionApplication on a class, and need to
access the instance of the class (which AutoCAD creates
for you), the easiest way is to add a static property, like
this:
public class PersistenceTest : IExtensionApplication
{
public PersistenceTest()
{
instance = this;
}
private static PersistenceTest instance = null;
public static PersistenceTest Instance
{
get
{
return instance;
}
}
public void Initialize()
{
// TODO Initialize your app here
}
public void Terminate()
{
// TODO: clean up here
}
}
Given the above, you can get the instance of the PersistenceTest
class that AutoCAD creates, by referencing the "Instance" property.
TonyT, MikeD, thanks for the help. Below is the test project that I created. Seems to work. Is there anything that I may want to do differently?
using System;
using System.Data;
using System.Windows.Forms;
using Autodesk.AutoCAD;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
namespace FilenameParse
{
public class PersistenceTest : Autodesk.AutoCAD.Runtime.IExtensionApplication
{
private bool _initiated = false;
public bool Status
{
get
{
return _initiated;
}
}
public void Initialize()
{
_initiated = true;
}
public void Terminate()
{
}
}
public static class PersistenceUI
{
private static FilenameParse.PersistenceTest _pt = new FilenameParse.PersistenceTest();
[CommandMethod("TestInitialize")]
public static void cmdInitialize()
{
if (_pt.Status == false)
{
_pt.Initialize();
}
}
[LispFunction("TestInitialize")]
public static ResultBuffer lspInitialize(ResultBuffer rb)
{
cmdInitialize();
return rb;
}
[CommandMethod("TestStatus")]
public static void cmdStatus()
{
MessageBox.Show("FilenameParse initialized: " + _pt.Status.ToString(), "FilenameParse.Status", MessageBoxButtons.OK);
}
[LispFunction("TestStatus")]
public static ResultBuffer lspStatus(ResultBuffer rb)
{
cmdStatus();
return rb;
}
}
}