Detect if Dynamo is running in Revit or not

I’m creating a set of WPF dialog nodes in C# for Dynamo and have a couple of related questions:

(1) Is there a way of detecting whether Dynamo is running in Revit, Sandbox or Studio mode?
(2) Why are threads treated differently in Dynamo Sandbox and Revit?

Details below:

In Sandbox mode,my node needs to call the dialog on a separate thread otherwise I get the following message:


I have therefore used the following code to call it which works fine:
DialogData myData = new DialogData() { SplashTitle = SplashTitle, SplashText = SplashText };
myData.InputList = myList;
//myData.openDialog();
ThreadStart dele = new ThreadStart(myData.openDialog);
Thread t = new Thread(dele);
t.SetApartmentState(ApartmentState.STA);
t.Start();
MessageBox.Show(“this far”);
t.Join();

However, if I run this in Dynamo Revit it crashes, but if I open the Dialog on the main thread it works (i.e. the approach that crashes in Dynamo Sandbox):
DialogData myData = new DialogData() { SplashTitle = SplashTitle, SplashText = SplashText };
myData.InputList = myList;
myData.openDialog();

For this reason, I need my node to detect how Dynamo is running so that it can modify its behaviour to suit.

Thanks!

I believe in DynamoRevit addins can only use the main thread (ui thread) and we only do work when revit is idle. There is an event which causes execution in Dynamo to occur.

In Dynamo have you tried using the WPF dispatcher to open your dialog?

Perhaps you could check if the list of loaded assemblies contains RevitAPI.

Thanks for your reply - I’m currently calling the dialog from a zerotouch node so don’t have access to DispatchOnUIThread. I’ll re-write as a full custom node and see if this works.

Thanks

Good idea - i’ll give thata go.

You can actually just do System.Diagnostics.Process.GetCurrentProcess().ProcessName which will either return “Revit” or “DynamoSandbox” (or probably “DynamoStudio” but I haven’t tested that).

1 Like

Even better, thanks.