private void progressTimer_Tick(object sender, EventArgs e) { if (!Directory.Exists(DownloadPath)) { return; } CurrentFile.Refresh(); long dirSize = completeFileDataDownloaded + CurrentFile.Length; double progress = (double)dirSize / (double)dataToDownload; if (progress > 1.0) { progress = 1.0; } progMain.Value = Convert.ToInt32(progress * (double)progMain.Maximum); // Transfer speed since last tick long transferred = dirSize - dataDownloadedSinceLastTick; dataDownloadedSinceLastTick = dirSize; if (transferred > 0 && stopwatch1.ElapsedMilliseconds > 0) { long transferRate = Convert.ToInt64((1000.0 / (double)(stopwatch1.ElapsedMilliseconds)) * (double)transferred); lblTransferRate.Text = HelperFunctions.SizeSuffix(transferRate) + "ps"; } stopwatch1.Restart(); // Average speed for title if (stopwatch2.ElapsedMilliseconds > 0) { long avg = Convert.ToInt64((double)dirSize / ((double)stopwatch2.ElapsedMilliseconds / 1000.0)); lblAvgTransferRate.Text = HelperFunctions.SizeSuffix(Convert.ToInt64(avg)) + "ps"; if (avg > 0) { // Time remaining TimeSpan remainingDuration = new TimeSpan(0, 0, (int)((dataToDownload - dirSize) / avg)); lblTimeRemaining.Text = remainingDuration.ToString("g"); } } }
public void getSizes() { var titlesCopy = titles.titles.Where(o => !titleSizes.Keys.Contains(o.titleID)).ToList(); var titlesCopy2 = titles.titles.Where(o => titleSizes.Keys.Contains(o.titleID)).ToList(); List <Task> workers = new List <Task> { }; for (var i = 0; i < 8; i++) { Task.Run(async() => { while (true) { TitleInfo item; lock (titlesCopy) { if (titlesCopy.Count == 0) { break; } item = titlesCopy[0]; titlesCopy.RemoveAt(0); } String contentSize; titleSizes.TryGetValue(item.titleID, out contentSize); if (contentSize == null || contentSize.Length == 0) { try { contentSize = HelperFunctions.SizeSuffix((await NUS.DownloadTMD(item.titleID)).TitleContentSize); } catch (Exception ex) { Debug.WriteLine(ex?.Message); Debug.WriteLine(ex?.InnerException?.Message); contentSize = ""; } if (titleSizes.ContainsKey(item.titleID) == false && contentSize != "") { titleSizes.Add(item.titleID, contentSize); } } if (contentSize != "") { this.Invoke((MethodInvoker) delegate { item.size = contentSize; frmList_SizeChanged(null, null); }); } } }); } Task.Run(() => { this.Invoke((MethodInvoker) delegate { lstMain.BeginUpdate(); foreach (TitleInfo item in titlesCopy2) { string size = ""; titleSizes.TryGetValue(item.titleID, out size); if (size != null) { item.size = size; } } frmList_SizeChanged(null, null); lstMain.EndUpdate(); }); while (workers.Count > 0) { ; } Common.Settings.cachedSizes = titleSizes; }); }