public static void RegisterDownload(MusixSongResult Download, CancellationTokenSource cancellationToken = null) { lock (Downloads) { Downloads.Add(new KeyValuePair <MusixSongResult, CancellationTokenSource>(Download, cancellationToken)); new Thread(x => { DownloadStarted?.Invoke(Download); DownloadsChanged?.Invoke(); }).Start(); } }
public static void TryReleaseDownload(MusixSongResult Download) { lock (Downloads) { int st = Downloads.Count; Downloads.RemoveAll(x => x.Key == Download); if (Downloads.Count != st) { new Thread(x => { DownloadFinished?.Invoke(Download); DownloadsChanged?.Invoke(); }).Start(); } } }
public ManagedDownload QueueImport(string name, Func <CancellationToken, IProgress <double>, Task <FileSample> > getImportTask) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException($"'{nameof (name)}' cannot be null or whitespace.", nameof(name)); } if (getImportTask is null) { throw new ArgumentNullException(nameof(getImportTask)); } var source = new CancellationTokenSource(); var download = new ManagedDownload(name); download.Task = SampleToHashTask(getImportTask(source.Token, download)); lock (this.downloads) this.downloads.Add(download); DownloadsChanged?.Invoke(this, EventArgs.Empty); return(download); }
public ManagedDownload QueueDownload(string id, string name, Task <Stream> stream, long length, string contentHash = null, IProgress <double> progress = null, CancellationToken cancellation = default) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException($"'{nameof (name)}' cannot be null or whitespace", nameof(name)); } if (string.IsNullOrWhiteSpace(id)) { throw new ArgumentException($"'{nameof (id)}' cannot be null or whitespace", nameof(id)); } if (stream is null) { throw new ArgumentNullException(nameof(stream)); } var download = new ManagedDownload(id, name); download.Task = DownloadCoreAsync(download, stream, length, contentHash, cancellation); lock (this.downloads) this.downloads.Add(download); DownloadsChanged?.Invoke(this, EventArgs.Empty); return(download); }