✅ Event-like event for ReadKey
with Console.ReadKey, the entire program waits for an input before continuing. is there a way to have an event similar to .
ReadKey
instead?
For example, after 5 seconds of no input from ReadKey
, I would like to have some random .WriteLine
text, BUT if i put the ReadKey
in an async method, after the .WriteLine is done, the program exits without ever getting the ReadKey
im considering to restructure my program, so that anything i want to happen ends up in a queue, then the entire program in a do while loop (exiting the loop exits the program).
then if theres just waiting for the input, the program will just loop over nothing repeatedly, still waiting for the ReadKey from the async Task13 Replies
BUT if i put the ReadKey in an async method, after the .WriteLine is done, the program exits without ever getting the ReadKeyChances ase that's because you let your
Main
method return? What decides when your application should exit?no my main method hasnt returned anything yet
here is an example of what im currently doing
im considering to restructure my program, so that anything i want to happen ends up in a queue, then the entire program in a do while loop (exiting the loop exits the program). then if theres just waiting for the input, the program will just loop over nothing repeatedly, still waiting for the ReadKey from the async Taskim considering to re-write it like this i solved it
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.UPDATE: I need a new method because there are flaws with a do while loop (or atleast the way im doing)
im not sure whats exactly happening, but I am currently using a do (while) loop, that will Task.Run tasks as they appear
nvm i fixed it again lol
looking at my picture helped me a bit
running thru the program fast enough caused it to sometimes attempt to run a task that wasnt there inside of the forEach loop (so if the there were 2 tasks in the list, it would run through those, but the do (while) loop would attempt to run thru 1 because I was clearing after all the tasks are done, and then the list would be cleared, and that unneccesary do(while) is trying to run on an empty list
Im talking to myself rn, but I would like feedback on how other people handle an event-based program
(this will never reach anyone im just talking to myself)
To be able to capture old input, make an observable.
https://stackoverflow.com/a/67995158/9731532
Change the readline to readkey for your case. That's how I'd do it
Stack Overflow
How to make an IObservable from console input
I have tried to write console observable as in the example below, but it doesn't work. There are some issues with subscriptions. How to solve these issues?
static class Program
{
static async T...
Also I think this is a case where you shouldn't use async programming but instead spawn a thread
since that ReadKey has to be blocking
okay
ill check it out
and update here if anything goes wrong
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.ya basically resolved cuz im off c# for some while and the project is done