Created
July 24, 2024 14:07
-
-
Save ricaun/7c7fcabb4cb320f9e69a3917e8e24d90 to your computer and use it in GitHub Desktop.
Simple Async ExternalEventHandler for Revit API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Autodesk.Revit.UI; | |
using System; | |
using System.Threading.Tasks; | |
public class AsyncExternalEventHandler : IExternalEventHandler | |
{ | |
private readonly Action<UIApplication> execute; | |
private readonly ExternalEvent externalEvent; | |
private TaskCompletionSource<bool> eventCompleted; | |
public AsyncExternalEventHandler(Action<UIApplication> execute) | |
{ | |
this.execute = execute; | |
this.externalEvent = ExternalEvent.Create(this); | |
} | |
public Task RaiseAsync() | |
{ | |
eventCompleted = new TaskCompletionSource<bool>(); | |
externalEvent.Raise(); | |
return eventCompleted.Task; | |
} | |
public void Execute(UIApplication app) | |
{ | |
try | |
{ | |
execute.Invoke(app); | |
eventCompleted.TrySetResult(true); | |
} | |
catch (Exception ex) | |
{ | |
eventCompleted.TrySetException(ex); | |
} | |
} | |
public string GetName() | |
{ | |
return this.GetType().Name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This command show the title of the document two times after a second wait.
AsyncExternalEventHandler need to be created inside the Revit API Context