Skip to content

Instantly share code, notes, and snippets.

@hugmouse
Created June 26, 2024 11:46
Show Gist options
  • Save hugmouse/34570b42a3b13b845b6ed3619d5c0fda to your computer and use it in GitHub Desktop.
Save hugmouse/34570b42a3b13b845b6ed3619d5c0fda to your computer and use it in GitHub Desktop.
I was just interested in trying out threads in C#, so here's just an example usage!
using System.Text.RegularExpressions;
partial class Program
{
static readonly HttpClient client = new();
static void Main(string[] args)
{
int threadCount = 10;
Thread[] threads = new Thread[threadCount];
Console.WriteLine($"Starting {threadCount} threads to get some random wikipedia articles!\n");
for (int i = 0; i < threadCount; i++)
{
threads[i] = new Thread(() => VisitRandomWikipediaPage(i));
threads[i].Start();
}
foreach (var Thread in threads)
{
Thread.Join();
}
Console.WriteLine("All threads are done for.");
}
static void VisitRandomWikipediaPage(int threadNum)
{
string url = "https://en.wikipedia.org/wiki/Special:Random";
try
{
HttpResponseMessage response = client.GetAsync(url).Result;
response.EnsureSuccessStatusCode();
string responseBody = response.Content.ReadAsStringAsync().Result;
Console.WriteLine($" [Thread {threadNum}] Visited a random Wikipedia page!");
// possible dereference here
Console.WriteLine($" [Thread {threadNum}] Redirected URL: {response.RequestMessage?.RequestUri}");
string title = ExtractTitle(responseBody);
Console.WriteLine($" [Thread {threadNum}] Page Title: {title}\n");
}
catch (HttpRequestException e)
{
Console.WriteLine($"\n [Thread {threadNum}] Exception caught umu!");
Console.WriteLine($" [Thread {threadNum}] Message :{0} ", e.Message);
}
}
static string ExtractTitle(string html)
{
var regex = TitleRegex();
var match = regex.Match(html);
if (match.Success)
{
// innerHTML of a h1 tag
string innerHtml = match.Groups[1].Value;
// Remove all HTML tags
return HTMLTagsRegex().Replace(innerHtml, "").Trim();
}
return "Title not found";
}
// Ну да, регекс для HTML, а что? Что ты мне сделаешь, а?
[GeneratedRegex("<h1[^>]*id=['\"]firstHeading['\"][^>]*>(.*?)</h1>", RegexOptions.Singleline)]
private static partial Regex TitleRegex();
[GeneratedRegex("<.*?>")]
private static partial Regex HTMLTagsRegex();
}
// Example output:
//
// Starting 10 threads to get some random wikipedia articles!
//
// [Thread 5] Visited a random Wikipedia page!
// [Thread 5] Redirected URL: https://en.wikipedia.org/wiki/Waterboarding
// [Thread 5] Page Title: Waterboarding
//
// [Thread 9] Visited a random Wikipedia page!
// [Thread 9] Redirected URL: https://en.wikipedia.org/wiki/TvG2
// [Thread 9] Page Title: tvG2
//
// [Thread 9] Visited a random Wikipedia page!
// [Thread 9] Redirected URL: https://en.wikipedia.org/wiki/William_Connor
// [Thread 9] Page Title: William Connor
//
// [Thread 2] Visited a random Wikipedia page!
// [Thread 2] Redirected URL: https://en.wikipedia.org/wiki/Manya_Joshi
// [Thread 2] Page Title: Manya Joshi
//
// [Thread 8] Visited a random Wikipedia page!
// [Thread 8] Redirected URL: https://en.wikipedia.org/wiki/Yekaterina_Strokova
// [Thread 8] Page Title: Yekaterina Strokova
//
// [Thread 3] Visited a random Wikipedia page!
// [Thread 3] Redirected URL: https://en.wikipedia.org/wiki/Excuse_Me_(Salvador_Sobral_song)
// [Thread 3] Page Title: Excuse Me (Salvador Sobral song)
//
// [Thread 6] Visited a random Wikipedia page!
// [Thread 6] Redirected URL: https://en.wikipedia.org/wiki/Christian_I._Nyby_II
// [Thread 6] Page Title: Christian I. Nyby II
//
// [Thread 4] Visited a random Wikipedia page!
// [Thread 4] Redirected URL: https://en.wikipedia.org/wiki/Jeanne_Galzy
// [Thread 4] Page Title: Jeanne Galzy
//
// [Thread 7] Visited a random Wikipedia page!
// [Thread 7] Redirected URL: https://en.wikipedia.org/wiki/Darren_Cato
// [Thread 7] Page Title: Darren Cato
//
// [Thread 1] Visited a random Wikipedia page!
// [Thread 1] Redirected URL: https://en.wikipedia.org/wiki/Olaf_M._Hustvedt
// [Thread 1] Page Title: Olaf M. Hustvedt
//
// All threads are done for.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment