Skip to content

Instantly share code, notes, and snippets.

@INTERNALINTERFERENCE
Created November 22, 2022 22:37
Show Gist options
  • Save INTERNALINTERFERENCE/92329e78d16b6165e3497887296cd84e to your computer and use it in GitHub Desktop.
Save INTERNALINTERFERENCE/92329e78d16b6165e3497887296cd84e to your computer and use it in GitHub Desktop.
namespace testchannels
{
internal class Program
{
private static readonly AsyncQueue<bool> _asyncQueue = new();
static async Task Main(string[] args)
{
await _asyncQueue.Enqueue(SetStateOn());
var key = Console.ReadKey().Key;
if (key == ConsoleKey.C)
{
await _asyncQueue.Enqueue(SetStateOff());
}
var anotherKey = Console.ReadKey().Key;
if (anotherKey == ConsoleKey.A)
{
}
}
private static bool SetStateOff()
{
var cancellationTokenSource = new CancellationTokenSource(
delay: TimeSpan.FromSeconds(5));
SetStateOff(cancellationTokenSource.Token);
return true;
}
private static bool SetStateOn( )
{
var cancellationTokenSource = new CancellationTokenSource(
delay: TimeSpan.FromSeconds(5));
SetStateOn(cancellationTokenSource.Token);
return true;
}
private static void SetStateOn(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
Console.WriteLine("hi");
}
}
private static void SetStateOff(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
Console.WriteLine("bye");
}
}
}
public class AsyncQueue<T>
{
private Channel<T> _channel;
public AsyncQueue()
=> _channel = Channel
.CreateUnbounded<T>();
public async Task Enqueue(T t)
{
await _channel.Writer.WriteAsync(t);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment