0

So I have a block of code like

        EventLog.WriteEntry("About to run the task");
        // run the dequeued task
        var task = PageRetrievals.Dequeue();
        PageRetrieval retrieval = new PageRetrieval();
        var continuation = task.ContinueWith(t => retrieval = t.Result);
        task.Wait();
        EventLog.WriteEntry("html = " + retrieval.Html.DocumentNode.OuterHtml);

where the WriteEntrys are just my sanity-check that this is working. But the 2nd isn't getting called and I'm trying to figure out why my code isn't reaching that point.

The above block of code is inside a method that is

MyTimer.Elapsed += new ElapsedEventHandler(MethodThatInvokesTheAboveCode);

and the type of task is like

        PageRetrievals.Enqueue(new Task<PageRetrieval>(() =>
            new PageRetrieval()
            {
                Html = PageOpener.GetBoardPage(pagenum),
                Page = PageType.Board,
                Number = pagenum
            }
        ));

where PageOpener.GetBoardPage simply gets the HTML from a URL, like

    private static HtmlDocument GetDocumentFromUrl(string url)
    {
        var webget = new HtmlWeb();
        var doc = webget.Load(url);
        return webget.StatusCode == System.Net.HttpStatusCode.OK ? doc : null;
    }

    public static HtmlDocument GetBoardPage(int pageNumber)
    {
        return GetDocumentFromUrl(string.Format(BoardPageUrlFormat, pageNumber));
    }

Is there anything about this that looks obviously wrong?

user7127000
  • 3,143
  • 6
  • 24
  • 41
  • 2
    In what kind of program are you executing this kind of code? Winforms? Wpf? Web? Console? Be aware that `task.Wait()` in a Winforms app will likely deadlock everything as the task requires the message loop to process messages in order for the task completion to finalize, and if you block the message loop waiting for a task to complete, you've created a deadlock, hence my question. – Lasse V. Karlsen Nov 28 '16 at 15:23
  • @LasseV.Karlsen This is in a Windows Service. I got the code for running a task to completion from http://stackoverflow.com/questions/13211334/how-do-i-wait-until-task-is-finished-in-c. – user7127000 Nov 28 '16 at 15:49

0 Answers0