jamietre / CsQuery

CsQuery is a complete CSS selector engine, HTML parser, and jQuery port for C# and .NET 4.
Other
1.16k stars 249 forks source link

ArgumentOutOfRangeException on multithreading #204

Open Israel4Ever opened 8 years ago

Israel4Ever commented 8 years ago

Hi, I'm running my code that working with CsQuery and getting this exception:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Call stack:

   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at CsQuery.HtmlParser.HtmlData.TokenName(UInt16 tokenId)
   at CsQuery.Implementation.DomElement.d__f.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at CsQuery.Implementation.DomElement.SetClassName(String className)
   at CsQuery.Implementation.DomElement.set_ClassName(String value)
   at CsQuery.Implementation.DomElement.SetAttribute(UInt16 tokenId, String value)
   at CsQuery.Implementation.DomElement.SetAttribute(String name, String value)
   at CsQuery.HtmlParser.CsQueryTreeBuilder.CreateElement(String ns, String name, HtmlAttributes attributes)
   at HtmlParserSharp.Core.TreeBuilder`1.AppendToCurrentNodeAndPushElement(ElementName elementName, HtmlAttributes attributes)
   at HtmlParserSharp.Core.TreeBuilder`1.AppendToCurrentNodeAndPushBodyElement(HtmlAttributes attributes)
   at HtmlParserSharp.Core.TreeBuilder`1.StartTag(ElementName elementName, HtmlAttributes attributes, Boolean selfClosing)
   at HtmlParserSharp.Core.Tokenizer.EmitCurrentTagToken(Boolean selfClosing, Int32 pos)
   at HtmlParserSharp.Core.Tokenizer.StateLoop(Int32 state, Char c, Int32 pos, Char[] buf, Boolean reconsume, Int32 returnState, Int32 endPos)
   at HtmlParserSharp.Core.Tokenizer.TokenizeBuffer(UTF16Buffer buffer)
   at CsQuery.HtmlParser.ElementFactory.Tokenize()
   at CsQuery.HtmlParser.ElementFactory.Parse(Stream inputStream, Encoding encoding)
   at CsQuery.HtmlParser.ElementFactory.Create(Stream html, Encoding streamEncoding, HtmlParsingMode parsingMode, HtmlParsingOptions parsingOptions, DocType docType)
   at CsQuery.Implementation.DomDocument.Create(Stream html, Encoding encoding, HtmlParsingMode parsingMode, HtmlParsingOptions parsingOptions, DocType docType)
   at CsQuery.CQ.CreateNew(CQ target, Stream html, Encoding encoding, HtmlParsingMode parsingMode, HtmlParsingOptions parsingOptions, DocType docType)
   at CsQuery.CQ..ctor(Stream html, Encoding encoding, HtmlParsingMode parsingMode, HtmlParsingOptions parsingOptions, DocType docType)
   at CsQuery.CQ.Create(Stream html, Encoding encoding, HtmlParsingMode parsingMode, HtmlParsingOptions parsingOptions, DocType docType)
   at ***.<>c__DisplayClass6_0.b__0() in ****:line 133

My working flow is:

  1. I'm downloading URL into local file.
  2. Calling Create() method to load this file into CQ object. Like this:
dom = CQ.Create(
    StreamToFile,
    encoding,
    HtmlParsingMode.Auto,
    HtmlParsingOptions.IgnoreComments
);

When I running same URL agian, this is pass successfuly without any exception. So I'm guess that this depends on some multi-threading conditions.

CsQuery version: v1.3.4 (latest via Nu-Get) .Net framework: v4.5.1 Visual studio 2015

How I can fix this?

Thank you.