β Task not returning and stopping program
Hey,
I am currently trying to start one task for every string in the list, but this isn't working and the program closes without actually calling the requested method nor displaying an error message.
Any help appreciated
38 Replies
how is the method being called?
This is the definition
And it's being called as part of this foreach
i need more context
99% of the time this problem is because main isn't awaiting the function call and just returns
Control Point B is never printed
right
i need to see how this method is called. the problem is not in that method
didnt you ask the same question yesterday?
yes
also you want to
ToArray()
this
without that it will run the tasks each time you enumerate itThis is the part where the method is called in Main()
Yes π
but I haven't made much progress hence
Control Point A is printing?
Yes
I have tried splitting the LINQ statement into a foreach and the whole program exits after trying to start the task
Which is weird as it runs perfectly fine synchronously
Might be a dumb question
but are you running this with a debugger
I mean I'm using Visual Studio
So yeah
Should I switch to release?
are you running it with the VS debugger though? it won't necessarily show you errors if you're not
usually it does, but i've had occasions where things just die for no reason
oh wait
var scrapingTask = await Task.Run(() => Scraping(list));
this should be var scrapingTask = await Scraping(list);
the way you have it awaits the start of Scraping
which happens instantly and then it continues onnope. That doesn't seem to be the problem
again your enumerating twice here
runnning the scraping twice
use
ToArray()
I have added it
This is now of type Task<Document>[]
what does this mean
Tested it and it still only reaches Point A
why arent your db calls async
youre not awaiting them either
The error seems to be
all your IO stuff should be async
youre wasting threads by running them sync then using Task.Run
and cluttering your code
and making it more complex for no reason
something's missing here
can i get the entirety of Main()
i really suggest you refactor all that then come back and implement this properly
Ok so I'd make them async and await them? I don't see why to await them. I just want to fire the push and then move on
Might be a good idea
fire and forget async code is a very bad idea, especially for io
you dont care if the push finishes?
or if it failed?
It has honestly become quite cluttered when implementing async as it was an afterthought
that will happen
it shouldnt have been an afterthought, youre doing a lot of IO
Yeah. This started out as a small project. I will need to take some time of weekend and not only rewrite the DBManager but also Scraper itself
yeah you should do that first
well. Thank you for your guidance. Do you know any useful guides to implementing it besides the MSDN article.
there's a stackoverflow regular that has a lot of really good blog posts about async stuff let me see if i can find his name
https://blog.stephencleary.com/ you'll have to search around for async specific stuff but what i've found of his has been very useful for me
Stephen Cleary (the blog)
Stephen Cleary's blog: async/await, programming, language design, and other sundry computer science topics.
Task.Run Etiquette and Proper Usage
I have a confession to make: I enjoy etiquette. My wife and I own a good dozen or two etiquette books, ranging from the classic Post to a more playful Austen-themed guide as well as several historical books (including an uncut copy of Social Life of Virginia in the Seventeenth Century). Thereβs a certain nerdy appreciation of knowing how to act ...
Thanks. I will look into this!
I'm going to close this thread then. Thanks for your help
Closed!