Reputation: 10551
There are several download requests which i want to serve one by one and for this reason i am using Queue. Here is the list, which is populating from different scripts at different times
public Queue<WebDownloader> webDownloaderQ;
A public method allow to different scripts/resource to populate the queue dynamically at different times:
public void EnqueABDownloading(WebDownloader abToDownload)
{
singleAbLoader.Enqueue(abToDownload);
}
The download starts quickly as it found even a single que object, (Remember during this time queue can be incremented),
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
}
}
I tried to write above function for downloading but the problem is, i dont know how to do this. I tried to used index with the object it saying cannot apply indexing with to an expression of type Queue.
EDIT:
Remember: I want to call one of the function (StartDowloaind
) of WebDownloader
before the Dequeue the object.
I want to first enqueue the request, the reason is that every request will take sometime to download. Therefore,
first enqueue the download request
then, check download loop is running or not if not then, initiate the loop
As specific download complete dequeue the request.
Upvotes: 1
Views: 251
Reputation: 643
please use Dequeue Method to pop up the item like the following
while (singleAbLoader.Count>0)
(
WebDownloader method= singleAbLoader.Dequeue();
method.StartDowloaind();
}
by this way even if the queue is incremented it will continue to use pop up and without the need for the index .
I also suggest to use ConcurrentQueue
if the queue will be used by multiple threads
Upvotes: 0
Reputation: 34150
You can use foreach
to loop through your queue. foreach
does not Dequeue
public void StartDownloading()
{
foreach(WebDownloader wd in singleAbLoader)
{
wd.Start();
}
}
Considering your comment:
As downloads may not complete in the order they start, so use a List
instead of Queue.
Your class WebDownloader
obviously has something like DownloadCompleted
event, you can remove it from the list in there. or if you need it, just then add it to a queue of completed downloads.
Upvotes: 1