public static Task ResetDownloadedInfo() { void clearBag <T>(ConcurrentBag <T> bag) { //Though not atomic, ResetHash() is called by only one thread. while (!bag.IsEmpty) { bag.TryTake(out var item); } }; clearBag(jsonHashs); clearBag(imageDatas); DownloadedCount = 0; return(Task.Run(() => { foreach (var file in new DirectoryInfo(Settings.DownloadPath).EnumerateFiles()) { var data = ImageProcesser.ExtractPixelData(ImageProcesser.LoadFromFile(file.FullName)); imageDatas.Add(data); if (file.Name.Contains("2018-02-17-bs-ba")) { testData = data; } } })); }
public Task Download() { return(Task.Run(() => { if (!IsInfoDownloaded) { return; } if (IsDownloaded || IsDownloading) { return; } try { using (var client = getWebClient()) { IsDownloading = true; if (!jsonHashs.Contains(Info.Hash)) { var rawData = client.DownloadDataAsTask(Info.DownloadUrl, tokenSource.Token).Result; rawData = ImageProcesser.AddMetadata(rawData, Info); var pixelData = ImageProcesser.ExtractPixelData(rawData); Debug.WriteLine($"Data fetched: {Info.LocaleName}-{rawData.GetHashCode()}"); if (!imageDatas.Contains(pixelData) && !containsData(pixelData)) { imageDatas.Add(pixelData); jsonHashs.Add(Info.Hash); ImageProcesser.SaveToFile(rawData, Info); Debug.WriteLine($"Downloaded: {Info.LocaleName}"); DownloadedCount++; } } } } catch (AggregateException ex) when(ex.InnerException is WebException) { return; } catch (Exception ex) when(ex is OperationCanceledException || ex is TaskCanceledException) { return; } #if !DEBUG catch (Exception) { return; } #endif finally { IsDownloading = false; //DownloadSpeed.Reset(); } })); }