예제 #1
0
        private static int GetMaxConnections(int maxThreads, string url)
        {
            RequestCounter requestCounter = new RequestCounter();

            List <RequestThread> threadState = new List <RequestThread>();

            for (int i = 0; i < maxThreads; i++)
            {
                threadState.Add(new RequestThread(new AutoResetEvent(false), url, requestCounter));
            }

            List <Thread> threads = new List <Thread>();

            foreach (RequestThread state in threadState)
            {
                Thread t = new Thread(StartRequest);
                t.Start(state);
                threads.Add(t);
            }

            WaitHandle[] handles = (from state in threadState select state.AsyncWaitHandle).ToArray();
            WaitHandle.WaitAll(handles, 5000);     // waits seconds

            foreach (Thread t in threads)
            {
                t.Abort();
            }

            foreach (RequestThread rs in threadState)
            {
                rs.Close();
            }

            return(requestCounter.Counter);
        }
예제 #2
0
 public RequestThread(AutoResetEvent r, String u, RequestCounter rc)
 {
     Url             = u;
     AsyncWaitHandle = r;
     RequestCounter  = rc;
 }