Tuesday, 23 June 2009

Multi-threading with the Revit API

One of my Revit plugins has been using a BackgroundWorker to run a resource-heavy and lengthy operation. Running an operation on a separate, dedicated thread like this enables you to keep the UI responsive where otherwise it might appear as though it has stopped responding, or at best be sluggish.

Until recently this all worked fine, until I came across a problem in a very particular scenario. I wanted to loop through RVTLinks elements in a model using a category filter, and then get the boundingbox data for them. The model was opened using the API's OpenDocumentFile. This resulted in the following complaint from Revit:



I sent a test app to A'desk and they replicated the problem. Apparently this is happening because the Revit API "doesn't support multi-threading", and there is an SPR logged for this. This is strange for several reasons:

1. I've been using multi-threading with no problems for a long time.
2. The same code works fine if I have opened the file manually, rather than through the API.
3. Surely every Revit programmer needs multi-threading, as inherently a lot of the things we do are at some point resource-heavy? Or do I have to go back to the old DoEvents()?

Hopefully I'll get a bit more info on this soon.

3 comments:

  1. Are you sure you're not disposing of Revit objects before the background thread has finished processing them? You have to be VERY careful using Backgroundworker with Revit.
    FWIW, threadsafe Revit API is no small feat given Revit's architecture. Don't hold your breath.

    ReplyDelete
  2. I think something being unavailable to my background thread when I'm expecting it to be is the crux here. But I'm using the same code I've used for umpteen months, and its only now that I'm trying to access a particular type of element, when all else is the same, that this error has arisen.

    "threadsafe Revit API is no small feat given Revit's architecture" - could you elaborate on this a little? :)

    ReplyDelete
  3. Hi Ed,

    I have the same problem here when using multi-threading with Revit API.

    when i made changes to the existing modal. For example
    1)set CompoundStructure to the floor type and report the progress back to the UI thread
    or
    2)rotate the column using method .rotate() and report to progress bar in UI thread.

    Do you get any solution for this problem. Any helps is appreciated. Thanks

    ReplyDelete

Comments are moderated, so you'll have to wait a little bit before they appear!