-1
    public BarchartParser()
    {
        // Initialize list
        StockSymbols = new List<string>();

        // Add items
        ParseBarchart();
    }

this is the C'tor that calls the method

    private async void ParseBarchart()
    {
        try
        {
            #region Get Html Document

            // Get response from site
            HttpClient http = new HttpClient();
            var response = await http.GetByteArrayAsync(BARCHART_WEBSITE);
            /* Break or W/e happens on this line ^^^ */

            // Encode html response to UTF-8
            string source = Encoding.GetEncoding(BARCHART_ENCODING)
                                        .GetString(response, 0, response.Length - 1);

            // Get html
            HtmlDocument document = new HtmlDocument();
            document.LoadHtml(source);

            #endregion

            #region Get Data From Table

            // Get table containining stock info
            HtmlNode table = document.DocumentNode.Descendants()
                .Single<HtmlNode>
            (
                x => (x.Name == "table") &&
                     (x.Attributes["class"] != null) &&
                     (x.Attributes["class"].Value.Equals("datatable ajax")) &&
                     (x.Attributes["id"].Value.Equals("dt1"))
            );

            // Get 'tbody' element from table
            HtmlNode tbody = table.Descendants("tbody").FirstOrDefault();

            // Get all rows from the table
            List<HtmlNode> allStocks = tbody.Descendants("tr").ToList();

            // For each row, id is "td1_X" where X is the symbol of the stock
            foreach (HtmlNode row in allStocks)
            {
                StockSymbols.Add(row.Attributes["id"].Value.ToString()
                    .Split(new char[] { '_' })[1]);
            }

            #endregion
        }
        catch
        {
            StockSymbols = new List<string>();
            StockSymbols.Add("this didn't work");
        }
    }

And the code from a simple form application that uses this:

    BarchartParser barchartData;

    public Form1()
    {
        InitializeComponent();
        barchartData = new BarchartParser();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        if (barchartData.StockSymbols != null && barchartData.StockSymbols.Count > 0)
            MessageBox.Show(barchartData.StockSymbols[0]);
        else
            MessageBox.Show("barchartData.StockSymbols is null or count == 0");
        this.Close();
    }

Not exactly sure what's going on here. It worked for one time that I debugged and then it stopped working.
This code is part of a function that is called during a C'tor. When this throw or whatever happens,
It just continues to the next breakpoint that I set in debug mode... Anyone has a clue of what
May be the cause of this?

Edit: I know it's not a throw because the code in the catch block doesn't happen. It simply moves on

Just in general, i'm following this guide https://code.msdn.microsoft.com/Parsing-Html-using-C-721be358/sourcecode?fileId=122353&pathId=1834557721

Giora Guttsait
  • 1,279
  • 15
  • 29

2 Answers2

0

When an await statement throws an exception, only a try block can catch it. I suggest you add a try-catch or try-finally to catch the exception and handle it properly.

Philippe Paré
  • 4,279
  • 5
  • 36
  • 56
  • While possible explanation OP said - "this line not throws" - so it is unlikely the issue... – Alexei Levenkov Aug 17 '15 at 21:31
  • Sorry for not including it, it is surrounded by a try-catch. This is why I've stated it's not a throw – Giora Guttsait Aug 17 '15 at 21:31
  • Misread that, in this case, enable Common Language exceptions in the exceptions settings to catch all exceptions created by the application. Event those in try blocks. There might be something else deeper that throws but gets caugh before this code – Philippe Paré Aug 17 '15 at 21:34
  • I checked in the VS settings for Common Language and I found nothing. – Giora Guttsait Aug 17 '15 at 21:39
0

You are not await-ing async method so only synchronous portion of the method (basically up to first real await) will be executed as part of your constructor call and the rest will eventually run on some arbitrary tread (possibly bringing process down in case of exception).

Generally you can't call async methods from constructor without good chance of deadlock if you try to call .Result or .Wait() (await vs Task.Wait - Deadlock?). As an option you can see if Fire-and-forget with async vs "old async delegate" works for your case.

Proper fix would be to move async operation from synchronous method (like constructor) to explicit async method and call it accordingly.

Hacky fix (with likley deadlock):

public BarchartParser()
{   
    ...
    ParseBarchart().Wait();
}
Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179