private void addMediaItemRow(string title, string type, string url, string opts, string filePath) { var li = new ListViewItem(new string[] { title, "Waiting", type, "-", "", "0%", "0.0 KB/s", url, filePath }); li.Tag = Common.RandomString(5) + DateTime.UtcNow.Ticks; listItems.Items.Add(li); Process p = YouTubeDL.run(opts); ProcessUpdateRow pur = new ProcessUpdateRow(); pur.proc = p; pur.item = listItems.Items[listItems.Items.Count - 1]; pur.results = new List <string> { "" // intentional }; dict.Add(li.Tag.ToString(), pur); int index = 0; // video if (type == "audio") { index = 1; } pur.item.ImageIndex = index; }
private void addMediaItemRow(string title, string type, string url, string opts, string filePath) { var li = new ListViewItem(new string[] { title, "Waiting", type, "-", "", "0%", "0.0 KB/s", url, filePath }); li.Tag = DateTime.Now.ToString("yyyyMMddhmmsstt"); listItems.Items.Add(li); Process p = YouTubeDL.run(opts); ProcessUpdateRow pur = new ProcessUpdateRow(); pur.proc = p; pur.item = listItems.Items[listItems.Items.Count - 1]; pur.results = new List <string> { "" // intentional }; Task.Run(() => { // spawns a new thread to read standard out data while (pur.proc != null && !pur.proc.HasExited) { pur.results.Add(pur.proc.StandardOutput.ReadLine()); } }); Task.Run(() => { // spawns a new thread to read error stream data while (pur.proc != null && !pur.proc.HasExited) { string line = pur.proc.StandardError.ReadLine(); if (!String.IsNullOrEmpty(line)) { pur.results.Add(line); } } }); dict.Add(li.Tag.ToString(), pur); int index = 0; // video if (type == "audio") { index = 1; } pur.item.ImageIndex = index; }
private void timerProcessLimit_Tick(object sender, EventArgs e) { int total = listItems.Items.Count; if (total > 0) { int active = 0; int done = 0; ProcessUpdateRow nextDownload = null; foreach (var pur in dict.Values) { if (pur.started && !pur.finished) { active += 1; } else if (pur.started && pur.finished) { done += 1; } else if (!pur.started && !pur.finished) { if (nextDownload == null) { nextDownload = pur; } } } if (total - done > 0) { if (active < settings.maxConcurrentDownloads) { foreach (var pur in dict.Values) { if (nextDownload != null && pur != nextDownload) { continue; } if (!pur.started) { pur.proc.Start(); pur.started = true; Task.Run(() => { // spawns a new thread to read standard out data while (pur.proc != null && !pur.proc.HasExited) { pur.results.Add(pur.proc.StandardOutput.ReadLine()); } }); Task.Run(() => { // spawns a new thread to read error stream data while (pur.proc != null && !pur.proc.HasExited) { string line = pur.proc.StandardError.ReadLine(); if (!String.IsNullOrEmpty(line)) { pur.results.Add(line); } } }); //active += 1; timerProcessLimit.Enabled = false; break; } } } } } }