저는 현재 사이트를 긁어서 XML 사이트 맵을 만드는 사이트 맵 생성기를 작성하고 있습니다. 대부분의 대기는 uri에 대한 요청에 소요되므로 스레딩을 사용하고 있습니다. 특히 ThreadPool 객체에서 빌드하는 것이 좋습니다.C#에서 알 수없는 양의 스레드 스레딩
주 스레드가 알려지지 않은 양의 스레드를 완료 할 때까지 대기하도록하기 위해 다음 설치를 구현했습니다. 나는 이것이 좋은 해결책이라고 생각하지 않는다. 어떤 스레딩 전문가가이 솔루션에 어떤 문제가 있는지 조언 해 주거나 그것을 구현하는 더 좋은 방법을 제안 할 수 있습니까?
EventWaitHandle 여기
EventResetMode.ManualReset
설정된다// Request first page (based on host)
Uri root = new Uri(context.Request.Url.GetLeftPart(UriPartial.Authority));
// Begin threaded crawling of the Uri
ThreadPool.QueueUserWorkItem(_waitCallback, root);
Thread.Sleep(5000); // TEMP SOLUTION: Sleep for 5 seconds
_eventWaitHandle.WaitOne();
// Server the Xml Sitemap
context.Response.ContentType = "text/xml";
context.Response.Write(GetXml().OuterXml);
어떤 아이디어가 많은 이해하는 메인 쓰레드 방법 :
에게 인 스레드있어서 여기protected void CrawlUri(object o)
{
try
{
Interlocked.Increment(ref _threadCount);
Uri uri = (Uri)o;
foreach (Match match in _regex.Matches(GetWebResponse(uri)))
{
Uri newUri = new Uri(uri, match.Value);
if (!_uriCollection.Contains(newUri))
{
_uriCollection.Add(newUri);
ThreadPool.QueueUserWorkItem(_waitCallback, newUri);
}
}
}
catch
{
// Handle exceptions
}
finally
{
Interlocked.Decrement(ref _threadCount);
}
// If there are no more threads running then signal the waithandle
if (_threadCount == 0)
_eventWaitHandle.Set();
}
인
매우 흥미로 웠습니다. 고맙습니다. 주말 내내 시험 사용해 볼 수있는 기회를 갖기를 바랍니다.나는 비동기 요청에 대해 잊어 버렸다고 믿을 수 없다. o – WDuffy