[C#] Eseguire contemporaneamente tutti i Thresad su "Multithread"

  • Creatore Discussione Creatore Discussione w_t
  • Data di inizio Data di inizio

w_t

Utente Attivo
3 Set 2007
121
0
16
Salve,
ho fatto un piccolo script Console Multithread per fare un parser su una pagina Html, ed estrarre tutti i link con class="pippo".

Codice:
        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.
 

Discussioni simili