✅ Using an async method to print contents of an array
I'm trying to make a simon game and the next step I've decided on is that I need a method to draw the simon map. I'm using an async method so that I can delay the time in which it takes to show the next map, but when I call the method it only shows the first map. When I use the code without putting it inside a method, it works fine. I don't understand what I'm doing wrong:
19 Replies
BlazeBin - ceosbjyrsfsv
A tool for sharing your source code with the world!
Can you include the code here?
sure
you're not awaiting
DrawMap
when you call itThe console probably ends as it's finished executing each line of code
also, in a console app like that, using the synchronous Thread.Sleep is fine
(it doesn't know you want to wait for a background process)
so when I call DrawMap(); I need to include an await after?
and gotcha on the thread.sleep, i saw on stackoverflow that people generally avoid it
it's bad in things like GUI apps where it would block the UI thread
or web apps where it would tie up threads that could be serving other requests
in console apps it's not an issue
before, not after
await DrawMap();
ah i see
ok thanks i'll see if i can implement thread.sleep instead
does that also need an await before the drawmap?
no
it's not async
got it
so in my case was my DrawMap(); method the background process?
It starts one and returns the Task used to track the task
ah i think i get it, that's why i needed to have the "await" before calling the method
Yep
Tasks are pretty complicated if you go deep into it. But mostly, await when you want to wait for it to complete :)
gotcha, thanks 😄
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.