-
-
Save ricaun/11b272c5bec46f05c3fe49525fcc3fdf to your computer and use it in GitHub Desktop.
IdlingQueueService is a class to run actions in the Idling Revit event in a queue.
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.Collections.Generic; | |
namespace RevitAddin | |
{ | |
/// <summary> | |
/// IdlingQueueService | |
/// </summary> | |
public class IdlingQueueService : IDisposable | |
{ | |
private readonly UIControlledApplication application; | |
private Queue<Action<UIApplication>> queue; | |
/// <summary> | |
/// IdlingQueueService | |
/// </summary> | |
/// <param name="application"></param> | |
public IdlingQueueService(UIControlledApplication application) | |
{ | |
this.application = application; | |
this.application.Idling += Application_Idling; | |
this.queue = new Queue<Action<UIApplication>>(); | |
} | |
/// <summary> | |
/// Add <paramref name="action"/> to run in <see cref="Autodesk.Revit.UI.UIApplication.Idling"/> | |
/// </summary> | |
/// <param name="action"></param> | |
public void Add(Action<UIApplication> action) | |
{ | |
this.queue.Enqueue(action); | |
} | |
/// <summary> | |
/// Application_Idling | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
private void Application_Idling(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e) | |
{ | |
var uiapp = sender as UIApplication; | |
if (queue.Count != 0) | |
{ | |
queue.Dequeue()?.Invoke(uiapp); | |
} | |
if (NeedToDispose) Dispose(); | |
} | |
#region Dispose | |
private bool NeedToDispose; | |
public void Dispose() | |
{ | |
NeedToDispose = true; | |
try | |
{ | |
this.application.Idling -= Application_Idling; | |
} | |
catch { } | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment