PDA

View Full Version : [C#] Eseguire contemporaneamente tutti i Thresad su "Multithread"


race2
23-11-2017, 19:00
Salve,
ho fatto un piccolo script Console Multithread per fare un parser su una pagina Html, ed estrarre tutti i link con class="pippo".



public static void Main()
{
Thread t;

for (int i = 0; i <= 10; i++)
{
DataThread data = new DataThread();

t = new Thread(new ParameterizedThreadStart(TestThread));

t.Start();
}

Console.ReadLine();
}



public static void TestThread(object data)
{
HtmlWeb htmlWeb = new HtmlWeb();
string sUrl = "";

HtmlDocument doc = htmlWeb.Load("https://www.pagina.com");

foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//a[@class='pippo']"))
{
sUrl = node.Attributes["href"].Value;

if (sUrl != "")
{
Console.Write(sUrl + "\n");
}
}



Lo eseguo, funziona tutto, solamente che esegue un thread alla volta, non partono tutti e 10 contemporaneamente.

Io vorrei lanciare 10 richieste http e poi il tempo che impiega ciascuna per terminare sono affari suoi, non mi deve bloccare l'esecuzione del prossimo thread e cioč della richiesta http.

Come posso fare ?

Grazie molte a tutti.