Edwin,
It may still be the case that I do not completely understand your approach, but I'll try to show you something. I suggest you take a look at the ModelessDialog samples in the SDK. They do, in a way, what you seem to be wanting. Here I show some snippets of the code just to illustrate what I mean (and meant in my previous post.)
If you look into the same, you will see the following method in the Application.cs (am an abbreviating the code here):
public Result OnShutdown(UIControlledApplication application)
{
if (m_MyForm != null && m_MyForm.Visible)
{
m_MyForm.Close();
}
return Result.Succeeded;
}
public Result OnStartup(UIControlledApplication application)
{
m_MyForm = null; // no dialog needed yet; the command will bring it
thisApp = this; // static access to this application instance
return Result.Succeeded;
}
public void ShowForm(UIApplication uiapp)
{
// If we do not have a dialog yet, create and show it
if (m_MyForm == null || m_MyForm.IsDisposed)
{
// A new handler to handle request posting by the dialog
RequestHandler handler = new RequestHandler();
// External Event for the dialog to use (to post requests)
ExternalEvent exEvent = ExternalEvent.Create(handler);
// We give the objects to the new dialog;
// The dialog becomes the owner responsible fore disposing them, eventually.
m_MyForm = new ModelessForm(exEvent, handler);
m_MyForm.Show();
}
}
As you can see, the Application has a variable m_MyForm, which is initially NULL. When a request comes (from other part of the add-in) to show the dialog, I check if it does not exist yet, and if that is the case I instantiate it and show it.
The request to show the dialog comes from an External command, just like in your case:
public virtual Result Execute(ExternalCommandData commandData
, ref string message, ElementSet elements)
{
try
{
Application.thisApp.ShowForm(commandData.Application);
return Result.Succeeded;
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}
The command does nothing but ask the Application to show the dialog, which will create it ans show it if it is not present yet. If the dialog already exists, the code does nothing.
Isn't this basically what you want?
Arnošt Löbel