-
-
Save INTERNALINTERFERENCE/92329e78d16b6165e3497887296cd84e to your computer and use it in GitHub Desktop.
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
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