Skip to content

Instantly share code, notes, and snippets.

@ricaun
Last active October 17, 2022 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricaun/11b272c5bec46f05c3fe49525fcc3fdf to your computer and use it in GitHub Desktop.
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.
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