Beispiel #1
0
 private void StartDownloadUnsafe(PreparedDownload download)
 {
     if (!registered.Contains(download))
     {
         download.StateChanged += dl_StateChanged;
         registered.Add(download);
         DownloadsAdded.RaiseEvent(this, new[] { download });
     }
     Directory.CreateDirectory(Path.GetDirectoryName(download.FilePath));
     active.Add(download);
     download.State = DownloadState.Active;
     download.Item.Module.StartDownload(download);
 }
Beispiel #2
0
        private void provider_ProcessDownloads(object sender, StUtil.Generic.EventArgs <IEnumerable <Download> > e)
        {
            DownloadProvider provider = (DownloadProvider)sender;
            var downloads             = e.Value.Select(dl => (PreparedDownload)Activator.CreateInstance(provider.PreparedDownloadType, dl)).ToArray();

            DownloadsAdded.RaiseEvent(this, downloads);

            foreach (var dl in downloads)
            {
                dl.StateChanged += dl_StateChanged;
                registered.Add(dl);
                try
                {
                    dl.State = DownloadState.Processing;
                    dl.Prepare(DownloadDirectory).ContinueWith(t =>
                    {
                        if (t.IsFaulted)
                        {
                            dl.LastError = t.Exception.InnerException ?? t.Exception;
                            dl.State     = DownloadState.Failed;
                        }
                        else
                        {
                            try
                            {
                                if (string.IsNullOrWhiteSpace(dl.FilePath))
                                {
                                    throw new FormatException("Download FilePath must be set to a valid path");
                                }
                                Path.GetFullPath(dl.FilePath);
                            }
                            catch (Exception ex)
                            {
                                dl.LastError = ex;
                                dl.State     = DownloadState.Failed;
                                return;
                            }
                            dl.State = DownloadState.Queued;
                            ProcessQueue();
                        }
                    });
                }
                catch (Exception ex)
                {
                    dl.LastError = ex;
                    dl.State     = DownloadState.Failed;
                }
            }
        }