C
C#17mo 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
canton717mo 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
kat206817mo ago
no my main method hasnt returned anything yet
kat2068
kat206817mo ago
kat2068
kat206817mo 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
Accord17mo 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
kat206816mo 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
kat206816mo ago
kat2068
kat206816mo 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
Anton16mo 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
Anton16mo 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
kat206816mo ago
okay ill check it out and update here if anything goes wrong
Accord
Accord16mo 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
kat206816mo 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
More Posts
✅ Outputting values from a listWhen typing in name and age it doesnt display the correct values. Instead it outputs: Program+PersonForcing user to choose 1, 2 or 3Hello, im a bit lost! first is a while-loop to make sure input is not null, but then how would I bes✅ Storing JSON and User data in a multi-tenant applicationHello! I'm developing a multi-tenant (multiple concurrent users) application. I require a database f❔ Deploying a dotnet core application with DockerI'm mostly used to building NodeJS applications where during development, I keep all "secrets" and a✅ Regex which matches strings containing things other than specific patternsThis is probably a very weird question (and also isn't strictly related to C#). I need a regex which❔ Is saving data from DB into private fields is a good idea?Lets say i have this class: ``` public class LessonAppService :ApplicationService, ILessonServi❔ .net 7, net 7.0.3 compatibility issues? in dockerI have following issue: i'm trying to run my application in docker, but it complains that i have no ❔ Binding a list<tuple<string,bool> to a form with switches in asp.netdoes anyone know how I can send a list of tuple string bool to a mvc page and display a checkbox in❔ Checking if ISymbol was autogeneratedHello, is there any way to check if `ISymbol` was autogenerated by c# compiler? In this case checkin❔ Hardlink in Linux .NET 6.0Dose anyone have a solution or guide to tell me how to essentially ln from within a .NET 6.0 console