Simulation or game loop using async-await and events [Answered]

I'm trying to simulate a system with participants where the system periodically notifies the participants to perform an action. I'm trying to use async/await with a custom Task delegate but exceptions aren't handled the way I thought they would be. Please refer to the Sharplab demo URL for more information: https://pastebin.com/J55Xzb6H. I have an exception for Id == 5 which does not get caught. If I uncomment the next < score exception it always gets caught for the listener with the biggest Id (i.e. the last one to be added to the list). Is there a better approach to do this? I wanted to leverage async/await to prevent having to wait for all the listeners to perform their actions one at a time but I do want to handle exceptions correctly. Full Sharplab URL: https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwMRQgG0wQwEaYCmABIVPkQLABQAAgEwCMNtADMbYwCwDcL7nAKx9qLAMwd6HRgHYaAbxrFlHCZwBsHABwdNAWRwBLKAAoAlEpWLqK28QCiAW0PBghMKWev3xALzEoQgB3cxE7FQAZQwBnYAAeKNiydwA+YkwYt0CwaL8A4NDLcIAzAHsPE2NgYkM81h4a4jjiRnqaxEQLG3Diax67DKTs6IA6AEE4OBNAkNrEFphPFzcwMzMwnoBfIrsyiqriACs6huPm4SOOrv6+/pVgMABPHZuXu+VaAE4l7zARpxchW6722wLuAGMcMBwQALYgmewAD3BhAADsBDKUoKRru9em93pxPiZCOsCXZQXiON9aOoRgARQi4R4mQSsVhksG2Sk9PaEHCw+EANxwHkwNWxgyy7miuPCt3emAZMVRpWihCB/R5ylBoPExDgTMIAHMoSRacQAGKGMCEMZQOCW8rGwjAMbRR5QcFxAAqAGUyIawIsffYhWQ3WBjdEUiZ/YH3AB+Yjqh3uENhiNjKO5UkifUMBxeFYKHa0CTW232x3O13uz3egG/RZNtxwTNQSPRlLJ1uEOAbZRltQ6C1N8w7BV2L66EYAJWgJnMfjSfbgiZGAEkoELSgBrDXAGExRYzEz0s0+wyOQgjABypSCa053Joeroaika47XdyIAc4adtm0almC5ZFss/Y/sB0Tnpe14kIQxb9hebhyviXIqGuqEkP4SGQXAOGDsQ2rEMOxA4VeN4Qd4hFmr0xAusADS6jQBZSIk0oeP+m70iqaoUIQoG2KiYCGCKbjELaOBwFimCPMQc44A6pSOMQAD6YBQMaeRni+KiieJ9HSbJUDyTRKwafhvzEeBnHJGAlSdjUcAtshHjWSs6FTrYm5wHkhgDuS6meT4eHucRtghe5/zIf5iD+AA8lAa7EaR5EHH5DFMSxOzkbQo6aMla4Iu5yapkGbkEdBOY4pO5KyRABAkIEiLVP4M60vOi7Lr4aSadp96EG19KlE1RDmPp4SNc1KbguUuHEKwIxaJFKiGMU8JZb4/iCGYxBHmAj75EEDjImiGJYiYAAkABE8h+ZsNS5FApTVK0ACEt1TXYAD0v01Jt0zDdUzTRPNtr7Ydx0zGdKLopiph3Q9cBPUd2D9ukj4+Eeym9ODC2bF9P22ESN33Y9Un8uCbYHaUpCAdUoX+VCvS3thZojFRhCJcUF6PJsIzfWleVgWoXAUfx6oTmCPkqNFkF/GuxAALRJSlcUi9QmxAA==
SharpLab
C#/VB/F# compiler playground.
10 Replies
Becquerel
Becquerel3y ago
that link doesn't seem to work, just fyi
Pobiega
Pobiega3y ago
your paste has expired
JansthcirlU
JansthcirlUOP3y ago
wtf already
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega3y ago
yup. and inside an iteration of the loop, you can run other things multithreaded, for when things inside that group don't care about execution logic
ero
ero3y ago
3 dots in the top right in the future
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
JansthcirlU
JansthcirlUOP3y ago
ah I see, I'll have a read then thanks so how could I restructure my code using async/await to have the desired outcome? I don't want to make my own threads and manage them cause I would probably mess up more i.e. have a game loop that fires an event periodically, and have listeners/actors that react to that event asynchronously and emit their own events for when their performed actions have finished this is a C++ question though, does C# try/catch behave the same?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server