KennanChan / Revit.Async

Use task-based asynchronous pattern (TAP) to run Revit API code from any execution context.
MIT License
223 stars 51 forks source link

Getter for UIApplication #24

Closed denizmaral closed 1 year ago

denizmaral commented 1 year ago

You can initialise the UIApplication in RevitTask class. It would be nice to get the uiApp from that static class because I would not have to pass for all windows and commands commandData. I pass for now UIControlledApplication in OnStartUp.

KennanChan commented 1 year ago

I don't get what you mean by "You can initialise the UIApplication in RevitTask class". Can u provide more detail?

denizmaral commented 1 year ago

Thank you very much for quick reply. I mean, in order to use UIApplication in windows etc. I have to pass it from ExternalCommand. image

In OnStartUp I implemented that Revit.Async.RevitTask.Initialize(application);

I would like to then get application variable through whole Application so as not to pass it for all commands.

denizmaral commented 1 year ago

I hope I explained it well :) It would be a nice helper to get uiApp if it was initialised. With doing that I won't have to worry about pass variable to all methods in all commands. For now I have to create for all windows a parameter with UIApplication and then I pass it to ViewModel to be able to create my ViewModel

KennanChan commented 1 year ago

You can get the instance of UIApplication by calling

RevitTask.RunAsync((uiApplication) => {
   // your code here
})

This way the instance of UIApplication is provided by Revit calling IExternalEventHandler.Execute method. For Revit.Async, it is not a good practise to hold an instance of UIAppliation and expose a getter.

KennanChan commented 1 year ago

In my point of view, it is good for memory management by getting an instance of UIApplication from IExternalEventHandler.Execute or RevitTask.RunAsync instead of passing it everywhere, because these are all local variables which is friendly to garbage collection. Keeping a reference of UIApplication instance in your code means a possible memory leak for Revit.

Revit.Async is designed to be a pure utility library which means it will focus on async operations in Revit API development and it is doomed to be stateless.

If you insist on holding an instance of UIApplication somewhere and use it everywhere, you can simple use a static class which exposes a static property.

public static class Global
{
      public static UIApplication UIApp { get; set; }
}

set it by Global.UIApp = uiApp once your plugin starts up.

denizmaral commented 1 year ago

Thank you very much for replies and support. Revit.Async is a great tool and I'm very happy to use it!