C
C#2y ago
GIGA BRAIN

✅ 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
GIGA BRAIN
GIGA BRAINOP2y ago
BlazeBin - ceosbjyrsfsv
A tool for sharing your source code with the world!
n8ta
n8ta2y ago
Can you include the code here?
GIGA BRAIN
GIGA BRAINOP2y ago
sure
GIGA BRAIN
GIGA BRAINOP2y ago
// Prints every map after a delay of one second
//foreach (string s in Renders)
//{
// Console.WriteLine(s);
// await Task.Delay(1000);
// Console.Clear();
//}
//Console.WriteLine("Complete!");


async Task DrawMap()
{
foreach (string s in Renders)
{
Console.WriteLine(s);
await Task.Delay(1000);
Console.Clear();
}
Console.WriteLine("Complete!");
}


DrawMap();
// Prints every map after a delay of one second
//foreach (string s in Renders)
//{
// Console.WriteLine(s);
// await Task.Delay(1000);
// Console.Clear();
//}
//Console.WriteLine("Complete!");


async Task DrawMap()
{
foreach (string s in Renders)
{
Console.WriteLine(s);
await Task.Delay(1000);
Console.Clear();
}
Console.WriteLine("Complete!");
}


DrawMap();
jcotton42
jcotton422y ago
you're not awaiting DrawMap when you call it
Tvde1
Tvde12y ago
The console probably ends as it's finished executing each line of code
jcotton42
jcotton422y ago
also, in a console app like that, using the synchronous Thread.Sleep is fine
Tvde1
Tvde12y ago
(it doesn't know you want to wait for a background process)
GIGA BRAIN
GIGA BRAINOP2y ago
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
jcotton42
jcotton422y ago
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();
GIGA BRAIN
GIGA BRAINOP2y ago
ah i see ok thanks i'll see if i can implement thread.sleep instead does that also need an await before the drawmap?
jcotton42
jcotton422y ago
no it's not async
GIGA BRAIN
GIGA BRAINOP2y ago
got it so in my case was my DrawMap(); method the background process?
Tvde1
Tvde12y ago
It starts one and returns the Task used to track the task
GIGA BRAIN
GIGA BRAINOP2y ago
ah i think i get it, that's why i needed to have the "await" before calling the method
Tvde1
Tvde12y ago
Yep Tasks are pretty complicated if you go deep into it. But mostly, await when you want to wait for it to complete :)
GIGA BRAIN
GIGA BRAINOP2y ago
gotcha, thanks 😄
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server