0
我有一段打開數據閱讀器的代碼,並且對於每個記錄(其中包含一個url)下載&進程處理該頁面。以多線程方式處理項目列表的最簡單方法
什麼讓它多線程,這樣,讓我們說,有可用於下載和simultaneousy程序頁面,10個插槽,並且插槽可用正在讀取下一行等
最簡單的方法我不能使用WebClient.DownloadDataAsync
這裏就是我試圖做的,但它沒有工作(即「工人」是絕然):
using (IDataReader dr = q.ExecuteReader())
{
ThreadPool.SetMaxThreads(10, 10);
int workerThreads = 0;
int completionPortThreads = 0;
while (dr.Read())
{
do
{
ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
if (workerThreads == 0)
{
Thread.Sleep(100);
}
} while (workerThreads == 0);
Database.Log l = new Database.Log();
l.Load(dr);
ThreadPool.QueueUserWorkItem(delegate(object threadContext)
{
Database.Log log = threadContext as Database.Log;
Scraper scraper = new Scraper();
dc.Product p = scraper.GetProduct(log, log.Url, true);
ManualResetEvent done = new ManualResetEvent(false);
done.Set();
}, l);
}
}
感謝您的輸入。我試過你所建議的,但仍然沒有運氣。 – Muxa 2009-02-05 03:14:18