我想一個一個地處理幾個下載請求,因此我使用隊列。這是列表,它在不同的時間從不同的腳本中填充 public Queue<WebDownloader> webDownloaderQ;公共方法允許不同的腳本/資源在不同時間動態(tài)填充隊列: public void EnqueABDownloading(WebDownloader abToDownload) { singleAbLoader.Enqueue(abToDownload); }下載開始很快,因為它甚至發(fā)現(xiàn)了一個隊列對象,(記住在此期間隊列可以遞增), public void StartDownloading() { //while (singleAbLoader.Count > 0) //{ // singleAbLoader. //} for (int i = 0; i < singleAbLoader.Count; i++) { singleAbLoader[i].//this is not supporting unable to call my method//I want to call one of the function (startdowloaind) of WebDownloader before the deque } }我試圖編寫上面的下載功能,但問題是,我不知道該怎么做。我嘗試將索引與對象一起使用,它說不能將索引應(yīng)用于 Queue 類型的表達式。編輯:記?。何蚁朐?Dequeue 對象之前調(diào)用其中一個函數(shù) ( StartDowloaind) 。WebDownloader我想先排隊請求,原因是每個請求都需要一些時間來下載。所以,首先排隊下載請求然后,檢查下載循環(huán)是否正在運行,如果沒有,則啟動循環(huán)作為特定的下載完成出列請求。
2 回答

慕雪6442864
TA貢獻1812條經(jīng)驗 獲得超5個贊
您可以使用foreach循環(huán)遍歷隊列。foreach不出隊
public void StartDownloading()
{
foreach(WebDownloader wd in singleAbLoader)
{
wd.Start();
}
}
考慮你的評論:
由于下載可能不會按開始順序完成,因此請使用 aList而不是 Queue。
你的班級WebDownloader顯然有類似DownloadCompleted事件的東西,你可以從那里的列表中刪除它?;蛘?,如果您需要,只需將其添加到已完成下載的隊列中。

qq_笑_17
TA貢獻1818條經(jīng)驗 獲得超7個贊
請使用 Dequeue 方法彈出項目如下
while (singleAbLoader.Count>0)
(
WebDownloader method= singleAbLoader.Dequeue();
method.StartDowloaind();
}
通過這種方式,即使隊列增加,它也會繼續(xù)使用彈出窗口,而不需要索引。
ConcurrentQueue如果隊列將被多個線程使用,我還建議使用
- 2 回答
- 0 關(guān)注
- 88 瀏覽
添加回答
舉報
0/150
提交
取消