Click here to Skip to main content
Sign Up to vote bad
good
See more: C#
I have a lab exercise. I do not understand urls.length and waitHandles.waitAll in this exercise.
 

 
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 19 Mar '12 - 1:23
Edited 19 Mar '12 - 1:38

Comments
BobJanova - 19 Mar '12 - 7:46
Did you write this code? And if so, how did you write it without understanding those things?
SAKryukov - 19 Mar '12 - 21:12
Exactly! --SA
Oshtri Deka - 19 Mar '12 - 8:22
This code has too many errors.

2 solutions

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.
  Permalink  
Comments
Shahin Khorshidnia - 19 Mar '12 - 18:37
+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[^]
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 8,356
1 OriginalGriff 6,571
2 CPallini 3,533
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 19 Mar 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid