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

Execute Console App from Revit #17

Closed yafimski closed 1 year ago

yafimski commented 2 years ago

Hi, I think this repo can be useful to execute a Console App from Revit, because the console app has async code, so I want to await the result of the console app before I trigger any ExternalEvent from Revit.

I'm new to this, so perhaps there is a simple way to explain how to: Start Revit Command -> Start Console App -> Await Result -> use Result in ExternalEvent -> do Transaction()

any tips on how to achieve this?

KennanChan commented 2 years ago

Well, it is a matter of wrapping an external process with task-based asynchronous pattern. The wrapper wraps around Process object just like Revit.Async wraps around Revit ExternalEvent

https://github.com/Cysharp/ProcessX This is a package I found on github who does similar wrapping

KennanChan commented 2 years ago

And your last part is right. When an external console app finishes running, you may get the result in a background thread. And you can then use Revit,Async to commit transaction on site easily

yafimski commented 2 years ago

And your last part is right. When an external console app finishes running, you may get the result in a background thread. And you can then use Revit,Async to commit transaction on site easily

Can this be done with the regular Revit.Async example, or do I need to use the example below Define your own handler in the readme? I just don't see where the 'process' comes in, for the vanilla example..

KennanChan commented 2 years ago

It's not part of Revit.Async, nor it is part of Revit.

You need to find solutions in the .NET world.

Actually the Microsoft .NET docs about System.Diagnostics.Process.Exited event demonstrates a simple async/await wrapper around Process object.

KennanChan commented 2 years ago

The key is to use TaskCompletionSource<T> class. It generates a Task<T> object for other logic to wait. And use TrySetResult<T>(T result) to signal that the Task<T> completes and other logic is can run.

KennanChan commented 1 year ago

Weakly related to Revit.Async, I think I can close this issue