i wanna get the content of 100 links as fast as possible. My first thought was to create one thread, that creates 100 Webbrowser objects, let them navigate and collect all html strings in a list. But when i try to run my code i get the error "actual thread is no singlethread-apartment".
I have the following Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
class ClassDriver
{
[STAThread]
public void StartDriver()
{
ClassTest t = new ClassTest();
Thread thread = new Thread(new ThreadStart(t.Collect));
thread.Start();
}
}
class ClassTest
{
private static List<WebBrowser> browsers;
private static List<string> htmls;
private static Stopwatch sw = new Stopwatch();
public void Collect()
{
string[] link = { "", "" };
sw.Start();
htmls = new List<string>();
browsers = new List<WebBrowser>();
for (int a = 0; a < 100; a++)
{
browsers.Add(new WebBrowser());
browsers.Last().DocumentCompleted += ClassGetRanking_DocumentCompleted;
browsers.Last().Navigate(link[0] + (a + 1) + link[1]);
}
}
private void ClassGetRanking_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser b = (sender as WebBrowser);
htmls.Add(b.DocumentText);
if (htmls.Count == browsers.Count)
{
sw.Stop();
}
}
}
}