title: How to set timeout for parser when it takes too long?
body: <p>I'm using ANTLR4 in C# with the following code sample:</p>
  <pre class="lang-cs prettyprint-override"><code>    AntlrInputStream antlrStream = new AntlrInputStream(text);
      MyLexer myLexer = new(new AntlrInputStream());
      myLexer.SetInputStream(antlrStream);
      CommonTokenStream myTokens = new CommonTokenStream(myLexer);
      parser = new MyParser(myTokens)
       {
         BuildParseTree = true,
       };
      IParseTree tree = parser.startRule();
  </code></pre>
  <p>Class <code>MyLexer</code>/<code>MyParser</code> are derived from the classes <code>Lexer</code>/<code>Parser</code> of Anlr4.Runtime and were auto generated by ANTLR4.</p>
  <p>In some rare cases, with specific text, <code>startRule()</code> takes forever and never finishes. I want to be able to set some kind of a &quot;Timeout&quot; for the parsing and throw an Exception.</p>
  <p>Any advice what is the recommended way to do it?</p>