Esempio n. 1
0
        public Exception Download(DownloadInfo downloadInfo)
        {
            IDownload sourceFilter = null;

            try
            {
                downloadThread        = System.Threading.Thread.CurrentThread;
                this.downloadResult   = 0;
                this.downloadFinished = false;
                this.cancelled        = false;

                sourceFilter = (IDownload) new MPUrlSourceSplitter();
                String url = UrlBuilder.GetFilterUrl(downloadInfo.Util, downloadInfo.Url);

                IDownload downloadFilter = (IDownload)sourceFilter;
                int       result         = downloadFilter.DownloadAsync(url, downloadInfo.LocalFile, this);
                // throw exception if error occured while initializing download
                Marshal.ThrowExceptionForHR(result);

                while (!this.downloadFinished)
                {
                    long total   = 0;
                    long current = 0;
                    if (downloadFilter.QueryProgress(out total, out current) >= 0)
                    {
                        // succeeded or estimated value
                        downloadInfo.DownloadProgressCallback(total, current);
                    }

                    // sleep some time
                    System.Threading.Thread.Sleep(100);

                    if (this.cancelled)
                    {
                        downloadFilter.AbortOperation();
                        this.downloadFinished = true;
                        this.downloadResult   = 0;
                    }
                }

                // throw exception if error occured while downloading
                Marshal.ThrowExceptionForHR(this.downloadResult);

                return(null);
            }
            catch (Exception ex)
            {
                return(ex);
            }
            finally
            {
                if (sourceFilter != null)
                {
                    Marshal.ReleaseComObject(sourceFilter);
                }
            }
        }