C
C#8mo ago
Raf Camora

✅ I need help in my code

If i press a key to return to the main menu then the console get cleared first and if i press a key again then i go back to the menu but i dont want to press 2 times i only want to press a key one time to go back to the menu
35 Replies
Raf Camora
Raf CamoraOP8mo ago
the code is in zaal.cs the method name is showcountdowntimer
Pobiega
Pobiega8mo ago
Hey, please upload the code to github or $paste
MODiX
MODiX8mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Pobiega
Pobiega8mo ago
We won't download a zip to help, you need to put the effort in to lower the barrier for potential helpers
Raf Camora
Raf CamoraOP8mo ago
GitHub
GitHub - RafCamora123/Cinema
Contribute to RafCamora123/Cinema development by creating an account on GitHub.
Raf Camora
Raf CamoraOP8mo ago
Does this work?
Pobiega
Pobiega8mo ago
yeah thats better I'm immediately confused as to why you have newtonsoft json in there modern C# mostly use System.Text.Json instead
Raf Camora
Raf CamoraOP8mo ago
oh im working with dotnet 8.0 or 7.0
Pobiega
Pobiega8mo ago
yeah. STJ has been available since .net 5
Raf Camora
Raf CamoraOP8mo ago
ohh okay Yeah so the problem is if the timer runs out and is 0 then i am not be able to press a key to go back to the menu from where i chose the options first
Pobiega
Pobiega8mo ago
well, the problem is likely related to your threading
Thread timerThread = new Thread(() => ShowCountdownTimer(remainingTime));
timerThread.Start();
Thread timerThread = new Thread(() => ShowCountdownTimer(remainingTime));
timerThread.Start();
because directly after starting that thread, I see this code
while (!chairsChosen && remainingTime > 0)
{
ConsoleKeyInfo keyInfo = Console.ReadKey();
while (!chairsChosen && remainingTime > 0)
{
ConsoleKeyInfo keyInfo = Console.ReadKey();
Raf Camora
Raf CamoraOP8mo ago
if you run the program you see the menu 1: show seats 2: show movies 3: show tickets 4: koffie 5: cancel reservation 6: quit and if you press 1 you can choose chairs but if the timer runs out then i cant go back to this menu 1: show seats 2: show movies 3: show tickets 4: koffie 5: cancel reservation 6: quit yes
Pobiega
Pobiega8mo ago
so you run a loop that expects input so the "first" keypress goes into that and the second one goes to the readline in ShowCountdownTimer
Raf Camora
Raf CamoraOP8mo ago
hmm how can i have the loop and that isnt expecting input?
Pobiega
Pobiega8mo ago
you might be able to quite easily fix this by not blocking unless there is something to read
Raf Camora
Raf CamoraOP8mo ago
it goes very wrong if i run the program if its 0 and press a key like for example h
Pobiega
Pobiega8mo ago
ConsoleKeyInfo keyInfo = Console.ReadKey(); can be prefixed with a if(Console.KeyAvailable)
Raf Camora
Raf CamoraOP8mo ago
and if i press on accident the downarrow then everything is gone its so weird i get now the errors oldcol, oldrow and keyinfo does not exist in the current context
Pobiega
Pobiega8mo ago
yeah you need to fix your scopes if creates a new block of code so you cant very well access the result of the read if the read never happened
Raf Camora
Raf CamoraOP8mo ago
oh its so hard 😦
Pobiega
Pobiega8mo ago
its only hard because you introduced threading doing console input (which is blocking) in a multithreaded application is a big no-no
Raf Camora
Raf CamoraOP8mo ago
okay
Pobiega
Pobiega8mo ago
you could have done the countdown differently, by capturing the "start" and then comparing now to start after each input if the time ran out, say "sorry, last input was ignored as the time ran out." bam, no threading needed
Raf Camora
Raf CamoraOP8mo ago
i have a lot of threading
Pobiega
Pobiega8mo ago
the problem is that if you currently have a ReadKey or similar active when another thread "wants to do something", it cant
Raf Camora
Raf CamoraOP8mo ago
i have now removed the threading but im not be able to select chairs https://paste.pythondiscord.com/S57Q
Pobiega
Pobiega8mo ago
it needs to wait for the read operation to finish
Raf Camora
Raf CamoraOP8mo ago
wait i forgot to remove the //thread comment
Pobiega
Pobiega8mo ago
if all you did was remove the threading, thats obviously not gonna work you cant just remove the "run in a separate thread" part and assume everything else will work
Raf Camora
Raf CamoraOP8mo ago
the timer runs but selecting doesnt work
Pobiega
Pobiega8mo ago
No description
Pobiega
Pobiega8mo ago
you'd need to re-write the whole choice loop logic I gotta go for a few hours, best of luck
Raf Camora
Raf CamoraOP8mo ago
!close
Accord
Accord8mo ago
Closed!
Want results from more Discord servers?
Add your server