0

For a Revit plugin I have written the following code:

public Result Execute(ExternalCommandData commandData,
                       ref string message, ElementSet elements)
    {
        try
        {
            Global.GetInstance(commandData);
            message = "Studio Launcher";
            var mw = new MainWindow();
            mw.ShowDialg();
        }
        catch (Exception)
        {
            TaskDialog.Show("Failure", "Please Open or Create a document");
            return Result.Failed;
        }
        return Result.Succeeded;
    }

In this plugin MainWindow is a ui that user interacts with and some transactions are take place there.
It works charm but the form is not modeless because the plugin is in the same thread as Revit itself. To provide a modeless window I changed the mw.ShowDialg(); to mw.Show();. Although the plugin starts successfully and MainWindow becomes modeless and user is able to interact with plugin and Revit at the same time, when I open a transaction in MainWindow, revit crashes because Execute() method is terminated before opening that transaction.

Please help me if there exist any way to develop a multi threaded plugin for revit.

a.toraby
  • 3,232
  • 5
  • 41
  • 73
  • What has the _modality_ of a window to do with a _threading_ model? –  Oct 25 '15 at 16:02

2 Answers2

5

See the Building Coder blog for a long list of discussions on the topic. If you're looking to just be able to have a modeless dialog then it's possible with ExternalEvents or Idling Events. However, if you are looking to run multiple threads concurrently then you are out of luck because the Revit API does not allow it.

Matt
  • 1,043
  • 5
  • 8
  • The workaround was a little strange and confusing at first glance. But I successfully achieved a modeless dialog using ExternalEvents finally. However I have some problems with `raise()` method from `ExternalEvent`. It does not provide any argument and all events invoked from my plugin have several event arguments. I will ask this in another question if I was not able to configure it. Thanks for your help. – a.toraby Oct 26 '15 at 07:39
3

Matt is absolutely right. The Revit API does not support multithreading at all. Use of the Revit API requires a valid API context, and that is only provided within Revit event handlers, e.g. Application.OnStartup, external event Execute, DMO Execute, etc.

The workaround is to use external events. An example is provided by the ModelessDialog/ModelessForm_ExternalEvent sample, and a long list of detailed discussions on this is provided by The Building Coder topic group on Idling and External Events for Modeless Access and Driving Revit from Outside:

http://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28

skeletank
  • 2,880
  • 5
  • 43
  • 75
Jeremy Tammik
  • 7,333
  • 2
  • 12
  • 17