Using backgroundworker with Revit API

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have a situation where revit is throwing an unmanaged error and I think I know why it is happening from reading Jeremy's blog posts on similar issues.
So, I have an external command that kicks off a Backgroundworker to download data from a database. The control return to Revit after the backgroundworker is kicked on. I am using a backgroundworker so my users would not have to wait until the data download is complete. I am subscribing to backgroundworker completed event to make some changes to the document. Revit API throws the error at this event. Here is what is likely happening:
1) The application/document objects are being disposed before the backgroundworker completed event is raised. I have made sure that I am pushing these objects to a static class, so they are available when the worker completes running. So this should not be an issue.
2) Multithreading should not be an issue because the background worker completed event should run on the same thread from where it is initialized. I am initializing it from the Revit thread.
3) From what I can figure out, Revit API does not like to be called from anywhere outside the standard events (external command, application start-up etc.). Is this right?
A possible workaround could be to execute a plug-in on demand from the backgroundworker completed event so the API calls stay within the confines. However, I cannot figure out a way from the documentation on how to execute an external command from other than the standard button click. Is this possible.
Could there be any workaround/solution for this. Thanks.