Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a lab exercise. I do not understand urls.length and waitHandles.waitAll in this exercise.



C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;


namespace LabEx1
{
    class Program
    {
        static void Main(string[] args)
        {
            AutoResetEvent[] waitHandles = new AutoResetEvent[urls.length];

            int i = 0;
            foreach (string url in urls)
            {
                waitHandles[i] = new AutoResetEvent(false);
                ThreadInfo ti = new ThreadInfo(url, waitHandles[i]);
                ThreadPool.QueueUserWorkItem(getPage, ti);
                i++;

                waitHandles.waitAll(waitHandles);
            }
        }
        class ThreadInfo
        {
            public string url;
            public AutoResetEvent are;

            public ThreadInfo(string _url, AutoResetEvent _are)
            {
                url = _url;
                are = _are;
            }
            static void getPage(object data)
            {
                ThreadInfo ti = (ThreadInfo)data;
                WebResponse wr = WebRequest.Create(ti.url).GetResponse();
                Console.WriteLine(ti.url + ": " + wr.Headers["Content-Length"]);
                wr.Close();
                ti.are.Set();
            }
        }
    }
}
Posted
Updated 19-Mar-12 1:38am
v2
Comments
BobJanova 19-Mar-12 7:46am    
Did you write this code? And if so, how did you write it without understanding those things?
Sergey Alexandrovich Kryukov 19-Mar-12 21:12pm    
Exactly!
--SA
Oshtri Deka 19-Mar-12 8:22am    
This code has too many errors.

Isn't google available where you are? Try googling "AutoResetEvent" and see what it tells you.

As far as the urls.Length issue is concerned, urls appears to be an array of strings, and it's passing the length of that array into AutoRresetEvent. Of course, if you had researched AutoResetEvent, you'd know why it's passing the length of the array as a parameter.
 
Share this answer
 
v2
Comments
Shahin Khorshidnia 19-Mar-12 18:37pm    
+5
check this link out.
http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx[^]

In a nutshell AutoResetEvent notifies a waiting thread that an event has occurred.

WaitAll
Waits for all the elements in the specified array to receive a signal
http://msdn.microsoft.com/en-us/library/z6w25xa6.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900