Hi Frank,
Thank you.
I start to think it is a Revit problem since somebody tells me it is never failed on Revit 2018.0, but it is sometimes failed on other versions.
I built a demo to simulate the problem, but I do not know how to submit it to Autodesk. The major code is as follows:
[Transaction(TransactionMode.Manual)]
public class ExternalCommand: IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
// create a new external event
TestEventHandler eh = new TestEventHandler();
Autodesk.Revit.UI.ExternalEvent e = Autodesk.Revit.UI.ExternalEvent.Create(eh);
if (null == e)
return Result.Failed;
ExternalEventRequest ret = e.Raise();
// this result seems always accepted
return Result.Succeeded;
}
}
[Transaction(TransactionMode.Manual)]
public class DummyCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
TaskDialog.Show("Test Event", "My job is started.");
return Result.Succeeded;
}
}
[Transaction(TransactionMode.Manual)]
public class TestEventHandler : IExternalEventHandler
{
private int m_nStep = 0;
private DateTime m_dtStamp = DateTime.Now;
public void Execute(UIApplication app)
{
// sometimes this function is not entered
// if the code is entered, the following processes are all correct
m_nStep = 0;
DoMyWork(app); // first step
app.Idling += OnIdle; // register the idle event
}
public string GetName()
{
return "Test Event";
}
private void OnIdle(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e)
{
UIApplication app = sender as UIApplication;
// wait for a few seconds until the previrous job is finished
TimeSpan span = DateTime.Now - m_dtStamp;
if (0.5 > span.TotalSeconds)
return;
switch (m_nStep)
{
case 1:
DoMyWork(app);
break;
case 2:
DoMyWork(app);
break;
case 3:
// the job is finished
app.Idling -= OnIdle;
TaskDialog.Show("Test Event", "My job is finished.");
break;
}
}
private void NextStep()
{
m_dtStamp = DateTime.Now;
++ m_nStep;
}
private void DoMyWork(UIApplication app)
{
// some code which need revit finish it, like run some commands
if (0 == m_nStep)
{
//RevitCommandId id = RevitCommandId.LookupCommandId("Bushman.TestExternalEvent.DummyCommand");
RevitCommandId id = RevitCommandId.LookupCommandId("b56a9b4b-27d7-4f41-aebf-deaa3b2ac674");
if (null != id && null != app)
app.PostCommand(id);
}
// next step
NextStep();
}
}