Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 2:23am
Edited 19-Mar-12 2:38am
v2
Comments
BobJanova at 19-Mar-12 7:46am
   
Did you write this code? And if so, how did you write it without understanding those things?
SAKryukov at 19-Mar-12 21:12pm
   
Exactly!
--SA
Oshtri Deka at 19-Mar-12 8:22am
   
This code has too many errors.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v2
Comments
Shahin Khorshidnia at 19-Mar-12 18:37pm
   
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
0 OriginalGriff 428
1 Maciej Los 249
2 BillWoodruff 199
3 /\jmot 180
4 Suraj Sahoo | Coding Passion 150
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 19 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100