Skip to content

Instantly share code, notes, and snippets.

@iluvadev
Created January 5, 2021 07:36
Show Gist options
  • Save iluvadev/6185306bea6a476bab7cf1ce17cf1045 to your computer and use it in GitHub Desktop.
Save iluvadev/6185306bea6a476bab7cf1ce17cf1045 to your computer and use it in GitHub Desktop.
Execute actions without block entire application, Example: NonBlockingAction.Add(() => Console.WriteLine(line));
public static class NonBlockingAction
{
private static BlockingCollection<Action> _Queue = new BlockingCollection<Action>();
static NonBlockingAction()
{
var thread = new Thread(
() =>
{
while (true) _Queue.Take()?.Invoke();
})
{
IsBackground = true
};
thread.Start();
}
public static void Add(Action action)
=> _Queue.Add(action);
public static void Add<T>(Func<T> function, Action<T> actionToDoWithResult)
=> Add(() => actionToDoWithResult(function()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment