Search code examples
c#streamwriter

using Steamwriter to write to a file gets stuck in foreach loop


I'm trying to create a small program that takes a html file as an input, and writes all links into a new output file. My problem is that the output file is empty. When debugging, it looks like I get stuck in the foreach loop. The code after the foreach loop is not being executed, and no exception is being thrown. What do I do wrong? It's a WinForms application, as I plan to implement more functions and a GUI.

I'll just paste the whole code that I have so far

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;

namespace Link_Grabber
{
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            string inputFilePath = GetInputFilePath();

            if (string.IsNullOrEmpty(inputFilePath))
            {
                MessageBox.Show("No input file selected.");
                return;
            }

            string outputFilePath = "output.txt";

            string htmlContent = File.ReadAllText(inputFilePath);

            string pattern = @"(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'"".,<>?«»“”‘’]))";
            MatchCollection matches = Regex.Matches(htmlContent, pattern);

            outputFilePath = GetOutputFilePath();

            if (string.IsNullOrEmpty(outputFilePath))
            {
                MessageBox.Show("No output file selected.");
                return;
            }

            try
            {
                using (StreamWriter writer = new StreamWriter(outputFilePath, false, Encoding.ASCII))
                {
                    writer.WriteLine("Entered writer");

                    foreach (Match match in matches)
                    {
                        writer.WriteLine(match.Value);
                        //(MessageBox.Show("Match found: " + match.Value);
                    }

                    MessageBox.Show("Before flush");
                    writer.Flush(); // Flush the writer to ensure content is written to the file
                    MessageBox.Show("After flush");

                    MessageBox.Show("Before close");
                    writer.Close(); // Close the writer to release the file resources
                    MessageBox.Show("After close");

                }

                MessageBox.Show("URLs extracted and saved to output.txt.");
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred while writing the file: " + ex.Message);
            }
        }

        static string GetInputFilePath()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "HTML Files (*.html;*.htm)|*.html;*.htm|All Files (*.*)|*.*";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                return openFileDialog.FileName;
            }

            return null;
        }

        static string GetOutputFilePath()
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                return saveFileDialog.FileName;
            }

            return null;
        }
    }
}

I want the URLs written to the output file. Without the foreach loop, it works fine. Sadly I can't spot the issue in the foreach loop.

EDIT:

when using Flush() inside the foreach loop, it works, and the content is written to the file. It still gets stuck after the loop, though. The cursor jumps to "in" in "foreach (Match match in matches)" and then nothing happens.

I'm using Visual Studio, .NET 6.8. I added the HtmlAgilityPack, rest is pretty default. The HTML I use for testing:


    <!DOCTYPE html>
<html prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#" lang="de">
    <head>
        <meta charset="utf-8" />
        <base href="//bs.to/" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="security_token" content="fbfae1fdcffb4284af9ba3b7" />
        <meta name="description" content="Schaue auf Burning Series mehr als 4000 Serien wie Die Simpsons, The Big Bang Theory und viele mehr gratis." />
        <meta name="keywords" content="bs, bs.to, burningseries.co, Burning Series, Serien, Streaming, Netflix, Die Simpsons, The Big Bang Theory, Two and a Half Men, How I Met Your Mother" />
        <meta name="robots" content="index, follow" />
        <meta property="og:type" content="website" />
        <meta property="og:title" content="The Boondocks (1) - Burning Series: Serien online sehen" />
        <meta property="og:url" content="https://bs.to/serie/The-Boondocks/1/de" />
        <meta property="og:image" content="https://bs.to/opengraph.jpg" />
        <meta property="og:description" content="Schaue auf Burning Series mehr als 4000 Serien wie Die Simpsons, The Big Bang Theory und viele mehr gratis." />
        <script type="text/javascript" nonce="876acb4897a8469fa9e5f855fe7" src="//local.adguard.org?ts=1684045558575&amp;type=content-script&amp;dmn=bs.to&amp;pth=%2Fserie%2FThe-Boondocks%2F1%2Fde&amp;app=chrome.exe&amp;css=3&amp;js=1&amp;rel=1&amp;rji=1&amp;sbe=1"></script>
<script type="text/javascript" nonce="876acb4897a8469fa9e5f855fe7" src="//local.adguard.org?ts=1684045558575&amp;name=AdGuard%20Extra&amp;name=AdGuard%20Popup%20Blocker&amp;type=user-script"></script><link href="/favicon.ico" type="image/x-icon" rel="shortcut icon" />
        <link href="/public/page.20230102164225.css" rel="stylesheet" />

        <link href="/public/custom4.css" rel="stylesheet" />

        
        <script src="/public/scripts/jquery.js"></script>
        <script src="/public/scripts/page.18dad0637254.js"></script>
        <title>The Boondocks (1) - Burning Series: Serien online sehen</title>
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
            <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div id="fb-root"></div>
        <div id="root">
            <header>
                <a href="/" class="banner"></a>
                <div class="fb-like" data-href="https://bs.to" data-layout="button_count" data-action="like" data-show-faces="false" data-share="true"></div>
                                    <form id="login" method="POST">
                        <input type="text" name="login[user]" placeholder="Username" required />
                        <input type="password" name="login[pass]" placeholder="Passwort" required />
                        <label><input type="checkbox" name="login[remember]" value="true" />&nbsp;Angemeldet bleiben</label>
                        <input type="hidden" name="security_token" value="fbf7e1ffefff4284af9ba3b7" />
                        <input type="submit" value="Login" />
                        <a href="registrierung">Registrieren</a>
                    </form>
                            </header>
            <nav>
                <div class="hamburger-container">
                    <div class="hamburger-name">Navigation</div>
                    <ul class="hamburger">
                        <li></li>
                        <li></li>
                        <li></li>
                    </ul>
                </div>
                <ul id="menu">
                    <li><a href="/">Home</a></li>
                    <li><a href="andere-serien">Alle Serien</a></li>
                    <li id="other-series-nav">
                        <a>Favoriten</a>
                        <ul>
                                                            <li><a href="serie/Die-Simpsons-The-Simpsons">Die Simpsons | The Simpsons</a></li>
                                                            <li><a href="serie/Family-Guy">Family Guy</a></li>
                                                            <li><a href="serie/Game-of-Thrones-GoT">Game of Thrones | GoT</a></li>
                                                            <li><a href="serie/How-I-Met-Your-Mother">How I Met Your Mother</a></li>
                                                            <li><a href="serie/South-Park">South Park</a></li>
                                                            <li><a href="serie/The-Walking-Dead">The Walking Dead</a></li>
                                                            <li><a href="serie/Two-and-a-Half-Men">Two and a Half Men</a></li>
                                                        <li><a href="vorgeschlagene-serien"><strong>Serienvorschläge</strong></a></li>
                        </ul>
                    </li>
                    <li>
                        <a href="andere-serien">Suche</a>
                        <ul>
                            <li><a href="andere-serien">nach Serien</a></li>
                            <li><a href="search">nach Episoden</a></li>
                        </ul>
                    </li>
                    <li><a href="https://board.bs.to" target="board">Forum</a></li>
                    <li>
                        <a>Mehr</a>
                        <ul>
                            <li><a href="https://cine.to" target="movies">Filme</a></li>
                            <li><a href="https://board.bs.to/index.php?/forum/30-serienkalender/" target="calendar">Serienkalender</a></li>
                            <li><a href="hilfe">Hilfe</a></li>
                            <li><a href="team">Team</a></li>
                            <li><a href="regeln">Regeln</a></li>
                            <li><a href="statistics">Statistiken</a></li>
                            <li><a href="random">Zufall</a></li>
                            <li><a href="https://burningseries.domains/"><strong>Domains</strong></a></li>
                        </ul>
                    </li>
                </ul>
            </nav>
            <section class="serie">
                                                                    
<div id="sp_left">
    <h2>
        The Boondocks
                                    <small>Staffel 1</small>
            </h2>
    <p>Als „Granddad“ Freeman das Sorgerecht für seine beiden wilden Enkel übertragen bekommt, zieht er mit Sack und Pack aus dem Süden Chicagos in die ruhige und sichere Nachbarschaft Woodcrest. Er hofft, die Jungs dort in Ruhe ignorieren zu können, um seine letzten Jahre in Frieden zu genießen. Doch weder Huey, ein linksgerichteter Revoluzzer, noch sein eigenwilliger 8-jähriger Bruder Riley sind mit dieser Veränderung zufrieden und beginnen die vorwiegend „weiße“ Nachbarschaft zu provozieren.</p>
    <div class="addthis_inline_share_toolbox"></div>
    <div class="infos">
        <div>
            <span>Genres</span>
            <p>
                                                    <span style="font-weight: bold;">Zeichentrick</span>
                            </p>
        </div>
        <div>
            <span>Produktionsjahre</span>
            <p>
                                <em>2005 - <i>Unbekannt</i></em>
            </p>
        </div>
        <div>
            <span>Hauptdarsteller</span>
            <p>
                                                    <span class="no-entry"><i>Keine Angabe</i></span>
                            </p>
        </div>
        <div>
            <span>Produzenten</span>
            <p>
                                                    <span>Aaron McGruder,</span>
                                                        <span>Rodney Barnes</span>
                                                </p>
        </div>
        <div>
            <span>Regisseure</span>
            <p>
                                                    <span>Bob Hathcock,</span>
                                                        <span>Seung Eun Kim,</span>
                                                        <span>Sung-hoon Kim</span>
                                                </p>
        </div>
        <div>
            <span>Autoren</span>
            <p>
                                                    <span>Aaron McGruder</span>
                                                </p>
        </div>
            </div>
</div>
<div id="sp_right">
    <img src="/public/images/cover/608.jpg" alt="Cover" />
    <a href="random/The-Boondocks/de"><span class="fas fa-fw fa-random"></span>Zufällige Episode</a>
    </div>
    
    <div class="clearfix"></div>
<script src="/public/scripts/series.js"></script>
<script src="/public/scripts/tippy.js"></script>
    
    <div class="selectors">
        <div class="language">
                            <strong>Sprachen</strong>
                <select class="series-language">
                                            <option value="de" selected>Deutsch</option>
                                    </select>
                    </div>
        <div class="seasons">
            <strong>Staffeln</strong>
            <div class="frame" id="seasons">
                <ul class="clearfix">
                                            <li class="s1"><a href="serie/The-Boondocks/1/de">1</a></li>
                                            <li class="s2"><a href="serie/The-Boondocks/2/de">2</a></li>
                                            <li class="s3"><a href="serie/The-Boondocks/3/de">3</a></li>
                                            <li class="s4"><a href="serie/The-Boondocks/4/de">4</a></li>
                                    </ul>
            </div>
            <div class="scrollbar">
                <div class="handle">
                    <div class="mousearea"></div>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    <script>series.init (1, 1, '');</script>
<table class="episodes">
                                        <tr class="">
            <td><a href="serie/The-Boondocks/1/1-The-Garden-Party/de" title="The Garden Party">1</a></td>
            <td>
                                <a href="serie/The-Boondocks/1/1-The-Garden-Party/de" title="The Garden Party">
                    <strong>The Garden Party</strong>
                </a>
            </td>
            <td>
                                                    <a href="serie/The-Boondocks/1/1-The-Garden-Party/de/VOE" title="VOE"><i class="hoster VOE"></i></a>
                                    <a href="serie/The-Boondocks/1/1-The-Garden-Party/de/UPStream" title="UPStream"><i class="hoster UPStream"></i></a>
                                    <a href="serie/The-Boondocks/1/1-The-Garden-Party/de/Vidoza" title="Vidoza"><i class="hoster Vidoza"></i></a>
                                    <a href="serie/The-Boondocks/1/1-The-Garden-Party/de/Streamtape" title="Streamtape"><i class="hoster Streamtape"></i></a>
                            </td>
        </tr>
                                        <tr class="">
            <td><a href="serie/The-Boondocks/1/2-The-Trial-of-Robert-Kelly/de" title="The Trial of Robert Kelly">2</a></td>
            <td>
                                <a href="serie/The-Boondocks/1/2-The-Trial-of-Robert-Kelly/de" title="The Trial of Robert Kelly">
                    <strong>The Trial of Robert Kelly</strong>
                </a>
            </td>
            <td>
                                                    <a href="serie/The-Boondocks/1/2-The-Trial-of-Robert-Kelly/de/VOE" title="VOE"><i class="hoster VOE"></i></a>
                                    <a href="serie/The-Boondocks/1/2-The-Trial-of-Robert-Kelly/de/UPStream" title="UPStream"><i class="hoster UPStream"></i></a>
                                    <a href="serie/The-Boondocks/1/2-The-Trial-of-Robert-Kelly/de/Vidoza" title="Vidoza"><i class="hoster Vidoza"></i></a>
                                    <a href="serie/The-Boondocks/1/2-The-Trial-of-Robert-Kelly/de/Streamtape" title="Streamtape"><i class="hoster Streamtape"></i></a>
                            </td>
        </tr>
                                        <tr class="">
            <td><a href="serie/The-Boondocks/1/3-Guess-Hoe-s-Coming-to-Dinner/de" title="Guess Hoe&#039;s Coming to Dinner">3</a></td>
            <td>
                                <a href="serie/The-Boondocks/1/3-Guess-Hoe-s-Coming-to-Dinner/de" title="Guess Hoe&#039;s Coming to Dinner">
                    <strong>Guess Hoe&#039;s Coming to Dinner</strong>
                </a>
            </td>
            <td>
                                                    <a href="serie/The-Boondocks/1/3-Guess-Hoe-s-Coming-to-Dinner/de/VOE" title="VOE"><i class="hoster VOE"></i></a>
                                    <a href="serie/The-Boondocks/1/3-Guess-Hoe-s-Coming-to-Dinner/de/UPStream" title="UPStream"><i class="hoster UPStream"></i></a>
                                    <a href="serie/The-Boondocks/1/3-Guess-Hoe-s-Coming-to-Dinner/de/Vidoza" title="Vidoza"><i class="hoster Vidoza"></i></a>
                                    <a href="serie/The-Boondocks/1/3-Guess-Hoe-s-Coming-to-Dinner/de/Streamtape" title="Streamtape"><i class="hoster Streamtape"></i></a>
                            </td>
        </tr>
                                        <tr class="">
            <td><a href="serie/The-Boondocks/1/4-Granddad-s-Flight/de" title="Granddad’s Flight">4</a></td>
            <td>
                                <a href="serie/The-Boondocks/1/4-Granddad-s-Flight/de" title="Granddad’s Flight">
                    <strong>Granddad’s Flight</strong>
                </a>
            </td>
            <td>
                                                    <a href="serie/The-Boondocks/1/4-Granddad-s-Flight/de/VOE" title="VOE"><i class="hoster VOE"></i></a>
                                    <a href="serie/The-Boondocks/1/4-Granddad-s-Flight/de/UPStream" title="UPStream"><i class="hoster UPStream"></i></a>
                                    <a href="serie/The-Boondocks/1/4-Granddad-s-Flight/de/Vidoza" title="Vidoza"><i class="hoster Vidoza"></i></a>
                                    <a href="serie/The-Boondocks/1/4-Granddad-s-Flight/de/Streamtape" title="Streamtape"><i class="hoster Streamtape"></i></a>
                            </td>
        </tr>
                                        <tr class="">
            <td><a href="serie/The-Boondocks/1/5-A-Date-with-the-Health-Inspector/de" title="A Date with the Health Inspector">5</a></td>
            <td>
                                <a href="serie/The-Boondocks/1/5-A-Date-with-the-Health-Inspector/de" title="A Date with the Health Inspector">
                    <strong>A Date with the Health Inspector</strong>
                </a>
            </td>
            <td>
                                                    <a href="serie/The-Boondocks/1/5-A-Date-with-the-Health-Inspector/de/VOE" title="VOE"><i class="hoster VOE"></i></a>
                                    <a href="serie/The-Boondocks/1/5-A-Date-with-the-Health-Inspector/de/UPStream" title="UPStream"><i class="hoster UPStream"></i></a>
                                    <a href="serie/The-Boondocks/1/5-A-Date-with-the-Health-Inspector/de/Vidoza" title="Vidoza"><i class="hoster Vidoza"></i></a>
                                    <a href="serie/The-Boondocks/1/5-A-Date-with-the-Health-Inspector/de/Streamtape" title="Streamtape"><i class="hoster Streamtape"></i></a>
                            </td>
        </tr>
                                        <tr class="">
            <td><a href="serie/The-Boondocks/1/6-The-Story-Of-Gangstalicious/de" title="The Story Of Gangstalicious">6</a></td>
            <td>
                                <a href="serie/The-Boondocks/1/6-The-Story-Of-Gangstalicious/de" title="The Story Of Gangstalicious">
                    <strong>The Story Of Gangstalicious</strong>
                </a>
            </td>
            <td>
                                                    <a href="serie/The-Boondocks/1/6-The-Story-Of-Gangstalicious/de/VOE" title="VOE"><i class="hoster VOE"></i></a>
                                    <a href="serie/The-Boondocks/1/6-The-Story-Of-Gangstalicious/de/UPStream" title="UPStream"><i class="hoster UPStream"></i></a>
                                    <a href="serie/The-Boondocks/1/6-The-Story-Of-Gangstalicious/de/Streamtape" title="Streamtape"><i class="hoster Streamtape"></i></a>
                            </td>
        </tr>
                                        <tr class=" disabled">
            <td><a href="serie/The-Boondocks/1/7-Merry-Christmas-Huey-Freeman/de" title="Merry Christmas, Huey Freeman">7</a></td>
            <td>
                                <a href="serie/The-Boondocks/1/7-Merry-Christmas-Huey-Freeman/de" title="Merry Christmas, Huey Freeman">
                    <strong>Merry Christmas, Huey Freeman</strong>
                </a>
            </td>
            <td>
                                            </td>
        </tr>
                                        <tr class="">
            <td><a href="serie/The-Boondocks/1/8-The-Reality/de" title="The Reality">8</a></td>
            <td>
                                <a href="serie/The-Boondocks/1/8-The-Reality/de" title="The Reality">
                    <strong>The Reality</strong>
                </a>
            </td>
            <td>
                                                    <a href="serie/The-Boondocks/1/8-The-Reality/de/VOE" title="VOE"><i class="hoster VOE"></i></a>
                                    <a href="serie/The-Boondocks/1/8-The-Reality/de/UPStream" title="UPStream"><i class="hoster UPStream"></i></a>
                                    <a href="serie/The-Boondocks/1/8-The-Reality/de/Vidoza" title="Vidoza"><i class="hoster Vidoza"></i></a>
                                    <a href="serie/The-Boondocks/1/8-The-Reality/de/Streamtape" title="Streamtape"><i class="hoster Streamtape"></i></a>
                            </td>
        </tr>
                                        <tr class="">
            <td><a href="serie/The-Boondocks/1/9-Return-of-the-King/de" title="Return of the King">9</a></td>
            <td>
                                <a href="serie/The-Boondocks/1/9-Return-of-the-King/de" title="Return of the King">
                    <strong>Return of the King</strong>
                </a>
            </td>
            <td>
                                                    <a href="serie/The-Boondocks/1/9-Return-of-the-King/de/VOE" title="VOE"><i class="hoster VOE"></i></a>
                                    <a href="serie/The-Boondocks/1/9-Return-of-the-King/de/UPStream" title="UPStream"><i class="hoster UPStream"></i></a>
                                    <a href="serie/The-Boondocks/1/9-Return-of-the-King/de/Streamtape" title="Streamtape"><i class="hoster Streamtape"></i></a>
                            </td>
        </tr>
                                        <tr class="">
            <td><a href="serie/The-Boondocks/1/10-The-It-is/de" title="The It is">10</a></td>
            <td>
                                <a href="serie/The-Boondocks/1/10-The-It-is/de" title="The It is">
                    <strong>The It is</strong>
                </a>
            </td>
            <td>
                                                    <a href="serie/The-Boondocks/1/10-The-It-is/de/VOE" title="VOE"><i class="hoster VOE"></i></a>
                                    <a href="serie/The-Boondocks/1/10-The-It-is/de/UPStream" title="UPStream"><i class="hoster UPStream"></i></a>
                                    <a href="serie/The-Boondocks/1/10-The-It-is/de/Vidoza" title="Vidoza"><i class="hoster Vidoza"></i></a>
                                    <a href="serie/The-Boondocks/1/10-The-It-is/de/Streamtape" title="Streamtape"><i class="hoster Streamtape"></i></a>
                            </td>
        </tr>
                                        <tr class="">
            <td><a href="serie/The-Boondocks/1/11-Let-s-Nab-Oprah/de" title="Let’s Nab Oprah">11</a></td>
            <td>
                                <a href="serie/The-Boondocks/1/11-Let-s-Nab-Oprah/de" title="Let’s Nab Oprah">
                    <strong>Let’s Nab Oprah</strong>
                </a>
            </td>
            <td>
                                                    <a href="serie/The-Boondocks/1/11-Let-s-Nab-Oprah/de/VOE" title="VOE"><i class="hoster VOE"></i></a>
                                    <a href="serie/The-Boondocks/1/11-Let-s-Nab-Oprah/de/UPStream" title="UPStream"><i class="hoster UPStream"></i></a>
                                    <a href="serie/The-Boondocks/1/11-Let-s-Nab-Oprah/de/Vidoza" title="Vidoza"><i class="hoster Vidoza"></i></a>
                                    <a href="serie/The-Boondocks/1/11-Let-s-Nab-Oprah/de/Streamtape" title="Streamtape"><i class="hoster Streamtape"></i></a>
                            </td>
        </tr>
                                        <tr class="">
            <td><a href="serie/The-Boondocks/1/12-Riley-Wuz-Here/de" title="Riley Wuz Here">12</a></td>
            <td>
                                <a href="serie/The-Boondocks/1/12-Riley-Wuz-Here/de" title="Riley Wuz Here">
                    <strong>Riley Wuz Here</strong>
                </a>
            </td>
            <td>
                                                    <a href="serie/The-Boondocks/1/12-Riley-Wuz-Here/de/VOE" title="VOE"><i class="hoster VOE"></i></a>
                                    <a href="serie/The-Boondocks/1/12-Riley-Wuz-Here/de/UPStream" title="UPStream"><i class="hoster UPStream"></i></a>
                                    <a href="serie/The-Boondocks/1/12-Riley-Wuz-Here/de/Vidoza" title="Vidoza"><i class="hoster Vidoza"></i></a>
                                    <a href="serie/The-Boondocks/1/12-Riley-Wuz-Here/de/Streamtape" title="Streamtape"><i class="hoster Streamtape"></i></a>
                            </td>
        </tr>
                                        <tr class="">
            <td><a href="serie/The-Boondocks/1/13-Wingmen/de" title="Wingmen">13</a></td>
            <td>
                                <a href="serie/The-Boondocks/1/13-Wingmen/de" title="Wingmen">
                    <strong>Wingmen</strong>
                </a>
            </td>
            <td>
                                                    <a href="serie/The-Boondocks/1/13-Wingmen/de/VOE" title="VOE"><i class="hoster VOE"></i></a>
                                    <a href="serie/The-Boondocks/1/13-Wingmen/de/UPStream" title="UPStream"><i class="hoster UPStream"></i></a>
                                    <a href="serie/The-Boondocks/1/13-Wingmen/de/Streamtape" title="Streamtape"><i class="hoster Streamtape"></i></a>
                            </td>
        </tr>
                                        <tr class="">
            <td><a href="serie/The-Boondocks/1/14-The-Block-is-Hot/de" title="The Block is Hot">14</a></td>
            <td>
                                <a href="serie/The-Boondocks/1/14-The-Block-is-Hot/de" title="The Block is Hot">
                    <strong>The Block is Hot</strong>
                </a>
            </td>
            <td>
                                                    <a href="serie/The-Boondocks/1/14-The-Block-is-Hot/de/VOE" title="VOE"><i class="hoster VOE"></i></a>
                                    <a href="serie/The-Boondocks/1/14-The-Block-is-Hot/de/Vidoza" title="Vidoza"><i class="hoster Vidoza"></i></a>
                                    <a href="serie/The-Boondocks/1/14-The-Block-is-Hot/de/Streamtape" title="Streamtape"><i class="hoster Streamtape"></i></a>
                            </td>
        </tr>
                                        <tr class="">
            <td><a href="serie/The-Boondocks/1/15-The-Passion-of-Reverend-Ruckus/de" title="The Passion of Reverend Ruckus">15</a></td>
            <td>
                                <a href="serie/The-Boondocks/1/15-The-Passion-of-Reverend-Ruckus/de" title="The Passion of Reverend Ruckus">
                    <strong>The Passion of Reverend Ruckus</strong>
                </a>
            </td>
            <td>
                                                    <a href="serie/The-Boondocks/1/15-The-Passion-of-Reverend-Ruckus/de/VOE" title="VOE"><i class="hoster VOE"></i></a>
                                    <a href="serie/The-Boondocks/1/15-The-Passion-of-Reverend-Ruckus/de/Vidoza" title="Vidoza"><i class="hoster Vidoza"></i></a>
                                    <a href="serie/The-Boondocks/1/15-The-Passion-of-Reverend-Ruckus/de/Streamtape" title="Streamtape"><i class="hoster Streamtape"></i></a>
                            </td>
        </tr>
    </table>
                            </section>
        </div>
        <footer>
            <ul>
                <li>&copy; 2023 Burning Series&trade;<small>Version 1.1.8</small></li>
                <li><a href="privacy">Privacy Policy</a></li>
                <li><a href="terms">Unsere AGB</a></li>
                <li><a href="statistics">Statistiken</a></li>
                <li><a href="team">Team</a></li>
                <li><a href="news">News</a></li>
            </ul>
        </footer>
                    <script>function f(b){var d=0;return function(){return d<b.length?{done:!1,value:b[d++]}:{done:!0}}}function h(b){var d="undefined"!=typeof Symbol&&Symbol.iterator&&b[Symbol.iterator];return d?d.call(b):{next:f(b)}}(function(b){5E3<(new Date).getTime()-(parseInt(window.location.hash.substring(1),28)||0)&&document.addEventListener("click",function k(c){c.preventDefault();var e=document.createElement("iframe");e.style.display="none";document.body.appendChild(e);for(var g=h(Object.entries(["open","setTimeout"])),a=g.next();!a.done;a=g.next())a=h(a.value),a.next(),a=a.next().value,Object.defineProperty(window,a,Reflect.getOwnPropertyDescriptor(e.contentWindow,a));c=new URL(c.target.href||window.location.href);c.hash="#"+(new Date).getTime().toString(28);c=window.open(c.href,c.href);window.setTimeout(function(){window.location="/"+b;window.blur()},50);c.focus();document.removeEventListener("click",k,!1)})})(document.cookie.replace(/(?:(?:^|.*;\s*)__bsduid\s*=\s*([^;]*).*$)|^.*$/,"$1"));</script>
                <script>!function(e,t,a,n,c){e.ym=e.ym||function(){(e.ym.a=e.ym.a||[]).push(arguments)},e.ym.l=+new Date,n=t.createElement(a),c=t.getElementsByTagName(a)[0],n.async=1,n.src="https://mc.yandex.ru/metrika/tag.js",c.parentNode.insertBefore(n,c)}(window,document,"script"),ym(72249604,"init",{clickmap:!0,trackLinks:!0,accurateTrackBounce:!0,webvisor:!0});</script>
        <script>
            (function(d, s, id) {
                var js, fjs = d.getElementsByTagName(s)[0];
                if (d.getElementById(id)) return;
                js = d.createElement(s); js.id = id; js.async = true;
                js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.7";
                fjs.parentNode.insertBefore(js, fjs);
            }(document, 'script', 'facebook-jssdk'));
        </script>

    </body>
    <script>
        $(document).ready(function() {
            //toggle menu
            $(".hamburger-container").click(function() {
                $("#menu").slideToggle();
            });

            //to fix issue that toggle adds style(hides) to nav
            $(window).resize(function() {
                if (window.innerWidth > 900) {
                    $("#menu").removeAttr("style");
                }
            });

            //icon animation
            var topBar = $(".hamburger li:nth-child(1)"),
                middleBar = $(".hamburger li:nth-child(2)"),
                bottomBar = $(".hamburger li:nth-child(3)");

            $(".hamburger-container").on("click", function() {
                if (middleBar.hasClass("rot-45deg")) {
                    topBar.removeClass("rot45deg");
                    middleBar.removeClass("rot-45deg");
                    bottomBar.removeClass("hidden");
                } else {
                    bottomBar.addClass("hidden");
                    topBar.addClass("rot45deg");
                    middleBar.addClass("rot-45deg");
                }
            });
        });
    </script>
</html>

<!-- Render: 8.957ms -->

Solution

  • The problem here is not in your C# code but rather your regular expression. Specifically, catastrophic backtracking is occurring, causing the evaluation of matches to take a very long time (effectively hanging your application).

    To see this, go to a regex test site such as Regex101 and test your regex against your HTML file. (You will need to escape the three forward slashes in your regex by changing each instance of / to \/.)

    The result will be:

    Catastrophic backtracking has been detected and the execution of your expression has been halted. To find out more and what this is, please read the following article: Runaway Regular Expressions

    You can also use the Regex101 debugger to step through and find the problem.

    In summary: your C# code is ok but your regex is not sufficiently robust to handle all HTML files safely. Perhaps consider a different approach, such as using a library to identify hyperlinks.