Ejemplo n.º 1
0
        /// <summary>
        /// Check Downloaders and start downloading.
        /// </summary>
        private static void CheckDownloaders()
        {
            // No limit
            if (MaxDownload == 0)
            {
                return;
            }
            DownloadersMutex.WaitOne();
            // Check downloaders
            List <Downloader> startingDownloaders = new List <Downloader>();

            for (int i = 0; i < Math.Min(Downloaders.Count, MaxDownload); ++i)
            {
                Downloader downloader = Downloaders[i];
                if (!downloader.IsActive && !downloader.IsFinished)
                {
                    startingDownloaders.Add(downloader);
                }
            }
            DownloadersMutex.ReleaseMutex();
            // Start downloading
            foreach (Downloader downloader in startingDownloaders)
            {
                downloader.DownloadAsync();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add a Downloader to Downloaders.
        /// </summary>
        /// <param name="downloader">The Downloader which is going to be added</param>
        /// <param name="handler">Handler for DownloadSucceeded event</param>
        /// <returns></returns>
        public static bool AddDownloader(Downloader downloader, DownloadCompletedEventHandler handler)
        {
            DownloadersMutex.WaitOne();
            // Clean up finished downloader
            int removedCount = Downloaders.RemoveAll(p => p.IsFinished);

            // Add downloader
            foreach (Downloader d in Downloaders)
            {
                if (d.To == downloader.To)
                {
                    d.DownloadSucceeded += handler;
                    DownloadersMutex.ReleaseMutex();
                    return(false);
                }
            }
            downloader.DownloadCompleted += new DownloadCompletedEventHandler(() =>
            {
                RemoveDownloader(downloader);
            });
            downloader.DownloadSucceeded += handler;
            // Add to downloaders
            Downloaders.Add(downloader);
            DownloadersMutex.ReleaseMutex();
            // Start downloading if no limit
            if (MaxDownload == 0)
            {
                downloader.DownloadAsync();
            }
            // Check downloaders
            CheckDownloaders();
            return(true);
        }