Is this the limitation of the code playground or did I miss something?
I've written a simple async/await program that should be working just fine unless i missed something critical. In fact sometimes i get the desired result other times it won't reach the end but does not break. I tried with and without delay. Removing the awit changes the result but still doesn't allways executes all the code. I'd be grateful if you can verify that I'm not losing my last bit of sanity. Or point out any major flaws with this little code piece https://code.sololearn.com/c8BLGCk4n8cw/?ref=app
10/19/2020 5:35:38 AMjack
7 AnswersNew Answer
Coder Kitten what confused me here is that my code as it is executes properly from time to time ( on certain occasions like Christmas and thanksgiving :P) but than again seems to just terminate midway. Which it shouldn't with or without logic to extend the runtime of main. Because main is clearly told to await task;) kinda like yield return to do something else and than come back ( just different)
My guess is that's exactly what's happening. As soon as Main finishes work it will terminate all other threads. Which would be why we get the full output if we keep main busy. In general i have the feeling that the Compiler got some problems with delaying/ sleep for a certain amount. What worries me is that even if you don't put the thread to sleep and just delegate the the output, the task does not execute. I created another showcase Method that returns an Action similiar to the Task in DoSomethingAsync that executes just fine in a Synced way. Just as i write this I'm thinking there might be a workarround here for multithreading in Sololearn. The problem here is we are using a Thread assigned to us from the threadpool, if we create a thread and start that thread manualy we can set its priority higher than Mains, which in turn will ensure that our code would execute as main will have the lower priority and therefor less time to process. This of cause is just a theory and needs to be tested.
@Code Kitten that explain why the code modest of the time only partailly executes, yet it leaves open how exactly the timespan before termination of a single thread is defined. Further it seems that the Code playground isn't compiling as straigth forward as I thought it would After reviewing my code and trying couple variants i found that: a.) the code does execute all of the start except the DoSomething() method if you dont await DoSomething(). b.)the code terminates at DoSomething() when awaiting DoSomething in start c.) code fully exectues if you await DoSomething in Main. (which confirms what you said earlier with the nested for loops) d.) code executes fully as intended in VS. For further notice if your async code is playing up or going all funny try another compiler first thanks for your valuable input on this topic. If it's up to me this thread is answered and closed