I got my extension almost running perfectly by have one more hurdle to overcome. If the extension is turned off when ArcMap starts, the user can turn on/off the extension and all the code runs fine. My problem occurs when the user leaves the extension turned on, exists ArcMap and then starts ArcMap up again. When this happens, the extension is already checked on in the Extension Manager dialog and the code that I need to have run, does not fire because ArcMap is not totally loaded at the time (IApplicationStatus).In the code below, CheckToolbarValidity has a couple of checks to verify that ArcMap is fully initialized and that the extension is turned on. If it passes these two checks, it calls SetupEvents which turns on the listeners for IActivewView::AddItem. Here lies my problem. If the extension is already turned on when ArcMap starts up, m_appStatus.Initialized returns false and the method exits before it can call SetupEvents.My extension follows the AcmeExt ESRI sample pretty closely with the main difference being that I don't call SetupEvents from IExtension::Startup. The reason I don't do this is because if did, the event listener would be turned on every time ArcMap starts regardless if my extension was turned on or not.I was thinking I could put a timer in CheckToolbarValidity so that it loops until m_appStatus.Initialized returns true. Problem here is that I never have tried to do timer in C# before.Does anyone think this is a bad idea and if so, do you have a suggestion for how to get around my problem?Thanks,Carlos public class Extension : IExtension, IExtensionConfig, IPersistVariant
{
private IApplication m_application;
private IMxDocument m_mxDocument;
private esriExtensionState m_extensionState = esriExtensionState.esriESDisabled;
private IApplicationStatus m_appStatus;
private IApplicationStatusEvents_Event m_mapStatusEvents;
private IActiveViewEvents_Event m_mapEvents;
#region IExtension Members
/// <summary>
/// Name of extension. Do not exceed 31 characters
/// </summary>
public string Name
{
get
{
return "RegGSS Extension";
}
}
public void Shutdown()
{
m_application = null;
}
public void Startup(ref object initializationData)
{
m_application = initializationData as IApplication;
if (m_application == null)
return;
m_appStatus = m_application as IApplicationStatus;
}
#endregion
#region IExtensionConfig Members
public string Description
{
get
{
return "RegGSS 9.3" +
"Copyright © SFWMD 2010" +
"Environmental Resource Regulation" +
"Regulatory Support - GIS Section";
}
}
/// <summary>
/// Friendly name shown in the Extension dialog
/// </summary>
public string ProductName
{
get
{
return "RegGSS";
}
}
public esriExtensionState State
{
get
{
return m_extensionState;
}
set
{
if (m_extensionState != 0 && value == m_extensionState)
return;
//Check if ok to enable or disable extension
esriExtensionState requestState = value;
if (requestState == esriExtensionState.esriESEnabled)
{
//Cannot enable if it's already in unavailable state
if (m_extensionState == esriExtensionState.esriESUnavailable)
{
throw new COMException("Cannot enable extension");
}
//Determine if state can be changed
esriExtensionState checkState = StateCheck(true);
m_extensionState = checkState;
CheckToolbarValidity();
}
else if (requestState == 0 || requestState == esriExtensionState.esriESDisabled)
{
//Determine if state can be changed
esriExtensionState checkState = StateCheck(false);
if (checkState != m_extensionState)
m_extensionState = checkState;
MessageBox.Show("Disabled");
UnloadEvents();
}
}
}
#endregion
/// <summary>
/// Determine extension state
/// </summary>
/// <param name="requestEnable">true if to enable; false to disable</param>
private esriExtensionState StateCheck(bool requestEnable)
{
//Turn on or off extension directly
if (requestEnable)
return esriExtensionState.esriESEnabled;
else
return esriExtensionState.esriESDisabled;
}
private void SetupEvents()
{
// Make sure we're dealing with ArcMap
if (m_application.Document.Parent is IMxApplication)
{
m_mxDocument = m_application.Document as IMxDocument;
m_mapEvents = m_mxDocument.FocusMap as IActiveViewEvents_Event;
m_mapEvents.ItemAdded += new IActiveViewEvents_ItemAddedEventHandler(OnItemAdded);
m_mapStatusEvents = m_application.Document.Parent as IApplicationStatusEvents_Event;
m_mapStatusEvents.Initialized += new IApplicationStatusEvents_InitializedEventHandler(OnInitialized);
}
}
// Called when the framework is fully initialized
// After this event fires, it is safe to make UI customizations
void OnInitialized()
{
CheckToolbarValidity();
}
void OnItemAdded(object Item)
{
//MessageBox.Show("Item Added");
IFeatureLayer pFeatureLayer;
if (Item is IFeatureLayer)
{
pFeatureLayer = Item as IFeatureLayer;
MessageBox.Show("Layer name: " + pFeatureLayer.Name);
}
}
private void CheckToolbarValidity()
{
// Wait for framework to initialize before making UI customizations
// Check framework initialization flag
if (!m_appStatus.Initialized)
return;
// Make sure the extension is enabled
if (m_extensionState != esriExtensionState.esriESEnabled)
return;
SetupEvents();
}
private void UnloadEvents()
{
//m_mapEvents.ItemAdded -= new IActiveViewEvents_ItemAddedEventHandler(OnItemAdded);
}
}