![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I would be grateful if you could help me out with a solution on how to force the code to wait for all methods to finish, here is the code, thank you: |
#3
| |||
| |||
|
|
Maya Sam wrote: I would be grateful if you could help me out with a solution on how to force the code to wait for all methods to finish, here is the code, thank you: Wow, that's some seriously ill-formatted code. Worse, it's missing a } or two, so it took a few minutes to get it to a readable state. Anyhow, probably the easiest way to do what you want is to not use an aysnch callback, but to 1) build two lists, of WebRequest-s and IAsyncResult-s 2) use BeginGetResponse to start each thread, passing null, null and then 3) use EndGetResponse to block until each has returned. public void ScanSites2(string str) { // Start each request List<WebRequest> Requests = new List<WebRequest>(URL.Length); List<IAsyncResult> Cookies = new List<IAsyncResult>(URLs.Length); foreach (string URL in URLS) { WebRequest request = HttpWebRequest.Create(URL); Requests.Add(request); Cookies.Add(request.BeginGetResponse(null, null)); } // Block until all return for (int Index = 0; Index < URLS.Length; Index++) UpdateItem2(Requests[Index].EndGetResponse(Cookies[Index]); } private void UpdateItem2(HttpWebResponse response) { // It would be much easier to use response.GetResponseStream().ReadToEnd() StringBuilder sb = new StringBuilder(4096); byte[] buf = new byte[2048]; int count; while ((count = response.GetResponseStream().Read(buf, 0, buf.Length - 8)) > 0) { sb.Append(Encoding.UTF8.GetString(buf, 0, count)); } StreamWriter sw = new StreamWriter("C:\\Rep\\" + state.URL); sw.Write(sb); sw.Close(); //here is the "res" variable that will hold the URL address concatenated | //and followed by another URL retrieved from another thread res += state.URL + "|"; } -- .NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net Delphi skills make .NET easy to learn Being printed - in stores by June |
#4
| |||
| |||
|
|
Apologies for the misforamtting, I didn't think of the fact that you might copy the code to try it at your side. |
|
I have tried your code and it worked in a very nice way, many thanks for that, one last question (I promise!), is there a way to catch the thread getresponse exceptions? for example I get alot of timeout error messages from sites I cant reach, the code doesn't do anything when a timeout occurs in the thread and it stays forever without breaking. any ideas how to get around this issue? |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |