PDA

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


race2
23-11-2017, 20: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.

Hardware Upgrade Forum Database Error
Database Error Database error
The Hardware Upgrade Forum database has encountered a problem.

Please try the following:
  • Load the page again by clicking the Refresh button in your web browser.
  • Open the www.hwupgrade.it home page, then try to open another page.
  • Click the Back button to try another link.
The www.hwupgrade.it forum technical staff have been notified of the error, though you may contact them if the problem persists.
 
We apologise for any inconvenience.