Created
January 5, 2021 07:36
-
-
Save iluvadev/6185306bea6a476bab7cf1ce17cf1045 to your computer and use it in GitHub Desktop.
Execute actions without block entire application, Example: NonBlockingAction.Add(() => Console.WriteLine(line));
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
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