I am trying to block RequestHandler.ParseAll() with await ConsumerTask;, but when i set a breakpoint there, i ALWAYS get the "Done..." output first... and then Parse2() fails with a NullReferenceException. (thats my guess: "the GC starts cleaning up because _handler got out of scope")
Anyway, I can't figure out why that happens.
class MainClass { public async void DoWork() { RequestHandler _handler = new RequestHandler(); string[] mUrls; /* fill mUrls here with values */ await Task.Run(() => _handler.ParseSpecific(mUrls)); Console.WriteLine("Done..."); } } static class Parser { public static async Task<IEnumerable<string>> QueryWebPage(string url) { /*Query the url*/ } public static async Task Parse1(Query query) { Parallel.ForEach(/*Process data here*/); } public static async Task Parse2(Query query) { foreach(string line in query.WebPage) /* Here i get a NullReference exception because query.WebPage == null */ } } sealed class RequestHandler { private BlockingCollection<Query> Queue; private Task ConsumerTask = Task.Run(() => /* call consume() for each elem in the queue*/); private async void Consume(Query obj) { await (obj.BoolField ? Parser.Parse1(obj) : Parser.Parse2(obj)); } public async void ParseSpecific(string[] urls) { foreach(string v in urls) Queue.Add(new Query(await QueryWebPage(v), BoolField: false)); Queue.CompleteAdding(); await ConsumerTask; await ParseAll(true); } private async Task ParseAll(bool onlySome) { ReInit(); Parallel.ForEach(mCollection, v => Queue.Add(new Query(url, BoolField:false))); Queue.CompleteAdding(); await ConsumerTask; /* Process stuff further */ } } struct Query { public readonly string[] WebPage; public readonly bool BoolField; public Query(uint e, IEnumerable<string> page, bool b) : this() { Webpage = page.ToArray(); BoolField = b; } }
Asyncsuffix) - are you able to cut it down at all?ParseSpecificreturns on its firstawaitstatement. You don'tawait ParseSpecific