C
C#14mo 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 BRAIN14mo ago
BlazeBin - ceosbjyrsfsv
A tool for sharing your source code with the world!
n8ta
n8ta14mo ago
Can you include the code here?
GIGA BRAIN
GIGA BRAIN14mo ago
sure
GIGA BRAIN
GIGA BRAIN14mo 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
jcotton4214mo ago
you're not awaiting DrawMap when you call it
Tvde1
Tvde114mo ago
The console probably ends as it's finished executing each line of code
jcotton42
jcotton4214mo ago
also, in a console app like that, using the synchronous Thread.Sleep is fine
Tvde1
Tvde114mo ago
(it doesn't know you want to wait for a background process)
GIGA BRAIN
GIGA BRAIN14mo 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
jcotton4214mo 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 BRAIN14mo 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
jcotton4214mo ago
no it's not async
GIGA BRAIN
GIGA BRAIN14mo ago
got it so in my case was my DrawMap(); method the background process?
Tvde1
Tvde114mo ago
It starts one and returns the Task used to track the task
GIGA BRAIN
GIGA BRAIN14mo ago
ah i think i get it, that's why i needed to have the "await" before calling the method
Tvde1
Tvde114mo 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 BRAIN14mo ago
gotcha, thanks 😄
Accord
Accord14mo 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
More Posts
❔ Merging the First Page of PDFs Using IText7 and C#Hello, i have written a function to merge the first page of one PDF into another using iTextShar✅ why is it '1' instead of '0'??So I am analysing this solution https://leetcode.com/problems/number-of-islands/solutions/3585795/c-❔ What's the best scope of a model in MAUI?This is my first .NET MAUI project. I'm trying to use SQLite with sqlite-net-pcl. My instinct is to ❔ OpenAPI add TAG swaggerHi, I have an api with two controllers and I'm using swashbuckle to generate the swaggers. What I doI'm having trouble with an IndexOf returning the position in a arrayI looked online and wasn't able to find a solution so any help would be appreciated.❔ How do I use make a button that shows data when pressed?Hi! Im trying to make phone book and I want the user to be able to press a button that shows the ent.NET Generic Host Writing "Application Started" when its done with its workSo I have been playing around with the Generic Host (Microsoft.Hosting.Extensions and HostedServices❔ How to recognize the client that connects to me in the API?I have a WebApi service that 2 other APIs are connecting to. How in the first one can I recognize wh❔ VOTING APPLICATION(HOMEWORKPLS HELP LAST 3 DAY THAK YOU )The program that is the chairman can send messages to all programs. Non-president programs can only ✅ Getting generic type parameters using reflectionUsing the reflection system, how can I get at the generic type parameters? I have the following: ```