私は、別々のスレッドでそれぞれ複数のWeb要求を処理するコンポーネントを持っています。各WebRequestの処理は同期しています。複数のWebRequestの管理におけるより良いアプローチ
public class WebRequestProcessor:System.ComponentModel.Component
{
List<Worker> tlist = new List<Worker>();
public void Start()
{
foreach(string url in urlList){
// Create the thread object. This does not start the thread.
Worker workerObject = new Worker();
Thread workerThread = new Thread(workerObject.DoWork);
// Start the worker thread.
workerThread.Start(url);
tlist.Add(workerThread);
}
}
}
public class Worker
{
// This method will be called when the thread is started.
public void DoWork(string url)
{
// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url);
// execute the request
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
// process stream
}
}
ここで、すべてのリクエストをキャンセルする最適な方法を見つける必要があります。
1つの方法は、各同期WebRequestを非同期に変換し、WebRequest.Abortを使用して処理をキャンセルすることです。
スレッドポインタを解放し、GCを使用してすべてのスレッドを終了させる方法もあります。
で言及として殺すのAppDomainを産卵考慮することです。それはスレッドの振る舞いではありません。あなたが作成した 'Thread'への参照がなくても、スレッドはまだ動いています。 – svick
質問は何ですか? – svick
はい彼らは完了した処理の後に死ぬでしょう、私の場合は最大20秒です – walter