C
C#2y ago
kat2068

✅ 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 Task
13 Replies
canton7
canton72y ago
BUT if i put the ReadKey in an async method, after the .WriteLine is done, the program exits without ever getting the ReadKey
Chances ase that's because you let your Main method return? What decides when your application should exit?
kat2068
kat2068OP2y ago
no my main method hasnt returned anything yet
kat2068
kat2068OP2y ago
kat2068
kat2068OP2y ago
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 Task
im considering to re-write it like this i solved it
Accord
Accord2y ago
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.
kat2068
kat2068OP2y ago
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
kat2068
kat2068OP2y ago
kat2068
kat2068OP2y ago
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)
Anton
Anton2y ago
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...
Anton
Anton2y ago
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
kat2068
kat2068OP2y ago
okay ill check it out and update here if anything goes wrong
Accord
Accord2y ago
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.
kat2068
kat2068OP2y ago
ya basically resolved cuz im off c# for some while and the project is done
Want results from more Discord servers?
Add your server