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; } } }
public void RegisterProvider(DownloadProvider provider) { providers.Add(provider); provider.ProcessDownloads += provider_ProcessDownloads; }