C
C#2y ago
Jona4Play

✅ Awaiting a method exits program

I don't why but awaiting this method will result in the code exiting with only ever reaching Checkpoint A. While this has let me to believe that there was some error in the method I'm calling I added a debug message. This debug message is also never displayed before the program exits. Same applies when creating a list of tasks and awaiting them with Task.WhenAll(). Is there anything super simple I'm missing?
foreach (var link in outstanding)
{
Console.WriteLine("Checkpoint A");
docs.Add(await Scraper.ScrapFromLinkAsync(link));
Console.WriteLine("Checkpoint B");
}
foreach (var link in outstanding)
{
Console.WriteLine("Checkpoint A");
docs.Add(await Scraper.ScrapFromLinkAsync(link));
Console.WriteLine("Checkpoint B");
}
26 Replies
phaseshift
phaseshift2y ago
Use the debugger. Sounds like there is an exception being thrown $debug
MODiX
MODiX2y ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Jona4Play
Jona4PlayOP2y ago
That is the weird part though. No exception thrown. It just exits out and never calls the method its supposed to
333fred
333fred2y ago
There is probably an exception being thrown. Debug->Windows->Exceptions, check the box for all managed exceptions, and try again
Jona4Play
Jona4PlayOP2y ago
You mean this?
Jona4Play
Jona4PlayOP2y ago
Still no exception thrown with these changes
Anton
Anton2y ago
you probably aren't awaiting the task of the method that this code is in try doing a try catch around that scraper and you'll see the exception
Jona4Play
Jona4PlayOP2y ago
try
{
docs.Add(await Scraper.ScrapFromLinkAsync(link));
}
catch (Exception)
{
throw;
}
try
{
docs.Add(await Scraper.ScrapFromLinkAsync(link));
}
catch (Exception)
{
throw;
}
Still no luck with this
phaseshift
phaseshift2y ago
That is kinda pointless
Jona4Play
Jona4PlayOP2y ago
It is, but it was upon the suggestion
phaseshift
phaseshift2y ago
You'll need to show more code
Jona4Play
Jona4PlayOP2y ago
Was about to
phaseshift
phaseshift2y ago
Particularly the entire async stack Please no async void
Jona4Play
Jona4PlayOP2y ago
It isnt 😅
Jona4Play
Jona4PlayOP2y ago
This is the method in Question.
phaseshift
phaseshift2y ago
$code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Jona4Play
Jona4PlayOP2y ago
and this is the main stack. cant use the code format as its to long
phaseshift
phaseshift2y ago
Then use a paste site
Jona4Play
Jona4PlayOP2y ago
ok sure
Jona4Play
Jona4PlayOP2y ago
Pastebin
Main: private static async void RunScraper() { //Called RunScra...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Jona4Play
Jona4PlayOP2y ago
It is ugly code still but I'll save refactoring for last
phaseshift
phaseshift2y ago
async void... First thing I see is async void You been telling porkies I need to head out for a bit
Jona4Play
Jona4PlayOP2y ago
bruh. Youre so right i am awaiting it in main but it obvously doesnt do anything
phaseshift
phaseshift2y ago
Yup You can't await it async Task, yes
Jona4Play
Jona4PlayOP2y ago
well huuuuge thanks to you. That might be the dumbest mistake I ever got stuck on for a whole day Closing the threat then. Have a great day
Want results from more Discord servers?
Add your server