❔ Form does not show up after using loop
My WindowsForm does not show up when using a
for(;;)
loop and I'm new to csharp. Any explanation??
9 Replies
Ah it's because you're using
Sleep
which blocks the main thread
@ohusq exchange Sleep(0.1);
for await Task.Delay(100);
And add the async
modifier to the ChangeRGB
methodThank you, works perfectly now
when do I make a function async and use await functions?
Honestly I don't fully understand what the async keyword does to a method. From what I've heard, it tells the compiler that once the code hits the
await
keyword it can make a continuation (basically puts a bookmark in and closes the book) and then works on other code until whatever task is completed
Then it picks up where it left off and finishes the method
Meanwhile sleep just patiently stops what it's doing and waitsAh, so it waits until a functions loop or code is done? (Which is indexed in the await)
Well the thing that's being awaited must always be either a
Task
object or an async
method (which compiles to return a Task
), so at a guess then C# stores a reference to that task and lets it run in the background (or shares processor time with it) while prioritising the rest of the program. Then once the task claims it's completed (or errored) then C# goes back and continues the ChangeRGB
method with the completed taskGot it, thank you
So while blocking the thread would be like boiling water and staring at it until it's done, using
async
would be where you'd spend the time chopping vegetables hahahaha, but 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.