C#C
C#2y ago
Emmanuel

await in Console App breaks my switch menu

Hi! So, I'm a .Net softdev student and I am building a facility booking app as a project, using C#, EF and SQL database. The app works great. But our teacher just came up with an extra feature and wants us to implement an async method somewhere in the code. I chose to do it to read bookings from the database. Here is my code in question, starting with the async method and the admin menu calling it.

If I ommit the 'await' in the method call in case 7, evertything works fine, when the method runs and ends but of course, the call is synchronous and is not interesting for my teacher. But when I use 'await' in case 7. The method is run well, the bookings are printed to the console. But that is when mayhem starts to happen. The menuChoice is broken when the method exits and the app is behaving very strangely, jumping between menus. I guess it has to do with how threads work with async-await but can't find any solution or help. I have been to deep on Stack I think I met Ada Lovelace, but she couldn't help. So I turn to higher powers. Anyone on this one? I have debugged for exceptions but everything is fine in that matter. The ViewBookingsAsync Task and the AdminMenuAsync Task are the only async methods in my whole code, might be worth mentioning.
image.png
image.png
Was this page helpful?