示例#1
0
        /// <summary>
        /// Downloads the specified URL.
        /// </summary>
        /// <param name="url">
        /// The download URL.
        /// </param>
        /// <param name="type">
        /// The download type.
        /// </param>
        /// <param name="section">
        /// The section.
        /// </param>
        /// <param name="skipCache">
        /// if set to <c>true</c> [skip cache].
        /// </param>
        /// <param name="cookieContainer">
        /// The cookie container.
        /// </param>
        /// <returns>
        /// The process download.
        /// </returns>
        public static string ProcessDownload(
            string url,
            DownloadType type,
            Section section,
            bool skipCache = false,
            CookieContainer cookieContainer = null)
        {
            bool found = false;

            if (url == null)
            {
                return(string.Empty);
            }

            var item = new DownloadItem
            {
                Type            = type,
                Url             = url,
                Section         = section,
                IgnoreCache     = skipCache,
                CookieContainer = cookieContainer
            };

            if (WebCache.CheckIfDownloadItemExistsInCache(item, true))
            {
                return(item.Result.Result);
            }

            try
            {
                lock (currentQueueLock)
                {
                    currentQueue++;
                }

                Downloading.Add(url);

                if (InBackgroundQue(url))
                {
                    RemoveFromBackgroundQue(url);
                }
            }
            catch (Exception ex)
            {
                Log.WriteToLog(LogSeverity.Error, 0, string.Format("Thread Download Setup {0}", url), ex.Message);
            }

            do
            {
                for (int i = 0; i < Get.Web.DownloadThreads; i++)
                {
                    if (!BackgroundWorkers[i].IsBusy)
                    {
                        try
                        {
                            item.ThreadID = i + 1;
                            item.Progress = Progress[i];

                            BackgroundWorkers[i].RunWorkerAsync(item);
                            found = true;

                            do
                            {
                                Application.DoEvents();
                                Thread.Sleep(50);
                            }while (BackgroundWorkers[i].IsBusy);

                            if (Downloading.Contains(url))
                            {
                                Downloading.Remove(url);
                            }

                            lock (currentQueueLock)
                            {
                                currentQueue--;
                            }

                            return(item.Result.Result);
                        }
                        catch (Exception ex)
                        {
                            Log.WriteToLog(LogSeverity.Error, 0, string.Format("Thread {0} Downloading {1}", i, url), ex.Message);
                        }
                    }
                }

                Application.DoEvents();
                Thread.Sleep(50);
            }while (found == false);

            lock (currentQueueLock)
            {
                currentQueue--;
            }

            return(null);
        }