Implement a Progress Bar and Abort a Lengthy Process

The time has come to talk of many things: of shoes and ships, and sealing-wax, of cabbages and kings
[^]…
or no, let’s continue sticking to the Revit API for now, more or less;
today’s topics are:

A Walk in the Snow

I went for a walk in snow and fog on a local hill and appreciated this wind-driven ice crystal covered baby tree that:

Wind-driven ice crystal covered baby tree

I thought I would like to share that with you.
Sorry that it is so dark, but that’s the way it was.

Implementing a Revit Add-in Progress Bar

I chatted with Harry Mattison on various topics during the winter break.
One of them was the implementation of a Revit add-in progress bar:

Question: I am trying to make a simple progress dialog based on your post on
modeless dialogues.
It seems to work well basically but has a few problems:

  1. The first time I run the command, the progress dialog flashes briefly on the screen and then disappears.
  2. The text label does not display properly.
  3. Sometimes the entire progress dialogue content turns white and the title bar says ‘Not Responding’.

Could you help me figure out if my approach is flawed or if it just needs some small adjustments?

Answer: Thank you, but no thank you… :-)

Instead, I propose that you look at my existing modeless progress bar implementation in the

ADN Revit MEP sample add-in
,
which has worked flawlessly for years now.

Response: Your progress bar is great!
I wish I had found it earlier.

Aborting a Lengthy Process

Here’s hopefully one last question about the progress bar:

How can the user cancel the operation from the progress bar?

Answer: You could implement some standard .NET stuff on the progress bar; maybe simply add a cancel button to it, and an event handler for that.

Response: After spending some time thinking about events and the like, I realized the progress bar cancel solution is almost trivial.

An idle event wouldn’t work in my case, because Revit was never going idle.
It was too busy doing hard work of exporting images for my Image-O-Matic application.
Instead, all I needed to do is add the Abort button to the progress dialog, and put this little bit of code in the progress bar class:


  private void btnAbort_Click(
    object sender,
    EventArgs e )
  {
    btnAbort.Text = "Aborting...";
    abortFlag = true;
  }
 
  public bool getAbortFlag()
  {
    return abortFlag;
  }

Then, inside my foreach loops, I just need to check the status of the flag:


  foreach( string phaseName in phaseList )
  {
    if( progressForm.getAbortFlag() )
      break;
 
    // . . .
  }

Many thanks to Harry for the fruitful discussion and helpful hint!

Image Generation Video Competition

Yet another item from Harry, this time a rather shameless plug, but I guess he earned it :-)

Harry announced a

competition
in
which you can win a free, custom-built Revit API add-in according to your own specifications for downloading his


Image-O-Matic
application,
generating a video with it, and uploading that to YouTube.

Have fun, and good luck to you!


Comments

4 responses to “Implement a Progress Bar and Abort a Lengthy Process”

  1. Joe Offord Avatar
    Joe Offord

    Hi Jeremy,
    I noticed the ProgressForm in your AdnRme solution uses Application.DoEvents. Yes, I agree this is required for the UI to recognize the user’s clicking of the abort button but I thought we discussed using Application.DoEvents was a big no-no for Revit. While the API may be chugging through a long sequence of code, calling Application.DoEvents during that sequence then allows the user to manipulate the main Revit window and even create new transactions using the UI. Am I wrong about this?

  2. Dear Joe,
    Thank you very much for noticing and pointing this out.
    You are right, of course, I think. I’ll ponder this a bit, and ask Harry what experiences he made with it so far. He should know by now if it causes any problems.
    Cheers, Jeremy.

  3. Dear Joe,
    Harry confirms:
    The progress bar does use DoEvents. If I try to start a new Transaction while my Image-O-Matic command is running and the progress bar is shown (for example by creating a wall) then an exception is thrown saying “Revit encountered an InvalidOperationException. Starting a new transaction is not permitted.”
    Cheers, Jeremy.

  4. Dear Joe,
    Arnošt clarifies the usage of DoEvents thus:
    We do not say that an addin should not call Appplication.DoEvents. All I claim is that Application.DoEvents does not help with Revit API events.
    Cheers, Jeremy.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading