public void Build(object files) { TorrentXMLHandler data = new TorrentXMLHandler(); List<Torrent> torrents = new List<Torrent>(); List<FileInfo> items = null; Torrent torrent; try { items = (List<FileInfo>)files; } catch { DirectoryHandler.LogError("Fatal Error converting object files to items in TorrentBuilder.Build");} information[14] = null; int filescount = items.Count; double progress = 0; double count = 0; foreach(FileInfo item in items) { string birth = GetTorrentBirth(item); ProcessTorrent(item, birth); if (information[14] != "true") { if (SettingsHandler.GetDownloadFormatExists(information[2])) { torrent = VerifyTorrent(); torrents.Add(torrent); data.AddTorrent(torrent); } } //Clear out information for this run to avoid misinformation on the next run for (int b = 0; b < information.Length; b++) information[b] = null; progress = (++count / filescount) * 100; if (progress <= 100 && progress >= 0) this.ReportProgress((int)progress); } ew.ClearApplyToAll(); }
//To be called only from DoWork public void MoveProcessedFiles() { string filepath; TorrentXMLHandler xml = new TorrentXMLHandler(); bool skip = false; int total = TorrentXMLHandler.table.Rows.Count; double progress = 0; double count = 0; List<DataRow> items = new List<DataRow>(); while (true) { DataTable copytable; if (this.CancellationPending) { lock (locker) { copytable = TorrentXMLHandler.GetProcessedRows(); } } //GET A COPY OF ALL NEWLY ADDED TORRENTS //and work with the copied table to allow TorrentXMLHandler to properly add/remove else { lock (locker) { copytable = TorrentXMLHandler.table.GetChanges(DataRowState.Added); } } if (copytable != null && copytable.Rows.Count > 0) { foreach (DataRow dr in copytable.Rows) { if (dr["Processed"] != DBNull.Value && (bool)dr["Processed"]) { if (!dr["File Path"].Equals(DBNull.Value)) { int index = copytable.Rows.IndexOf(dr); filepath = (string)dr["File Path"]; if (filepath.Contains("[CSL] -- Unhandled Torrents") && (bool)dr["Error"]) skip = true; else skip = false; if (!skip) { switch ((bool)dr["Error"]) { case true: filepath = MoveTorrentFile((string)dr["File Path"], "unhandled"); if (filepath == null) { copytable.Rows[index]["File Path"] = "Problem moving file.."; copytable.Columns["Error"].ReadOnly = false; copytable.Rows[index]["Error"] = true; copytable.Columns["Error"].ReadOnly = true; } else copytable.Rows[index]["File Path"] = filepath; break; case false: try { filepath = MoveTorrentFile((string)dr["File Path"], "handled"); if (filepath == null) { copytable.Rows[index]["File Path"] = "Problem moving file.."; copytable.Columns["Error"].ReadOnly = false; copytable.Rows[index]["Error"] = true; copytable.Columns["Error"].ReadOnly = true; } else copytable.Rows[index]["File Path"] = filepath; } catch (Exception e) { LogError(e.Message + "\n" + e.StackTrace); } break; default: filepath = MoveTorrentFile((string)dr["File Path"], "handled"); if (filepath == null) { copytable.Rows[index]["File Path"] = "Problem moving file.."; copytable.Columns["Error"].ReadOnly = false; copytable.Rows[index]["Error"] = true; copytable.Columns["Error"].ReadOnly = true; } else copytable.Rows[index]["File Path"] = filepath; break; } } } progress = (++count / total) * 100; if (progress <= 100 && progress >= 0) ReportProgress((int)progress); } }//END FOREACH //Load the copytable back into TorrentXMLHandler lock (locker) { TorrentXMLHandler.table.Columns["Error"].ReadOnly = false; foreach (DataRow dr in copytable.Rows) { DataRow find = TorrentXMLHandler.table.Rows.Find(dr["File"]); if (find != null) { int index = TorrentXMLHandler.table.Rows.IndexOf(find); try { TorrentXMLHandler.table.Rows[index]["File Path"] = dr["File Path"]; TorrentXMLHandler.table.Rows[index]["Error"] = dr["Error"]; } catch (Exception e) { DirectoryHandler.LogError(e.Message + "\n" + e.StackTrace); } } } TorrentXMLHandler.table.Columns["Error"].ReadOnly = true; try { TorrentXMLHandler.table.AcceptChanges(); } catch (Exception e) { DirectoryHandler.LogError(e.Message + "\n" + e.StackTrace); } } } if (this.CancellationPending) { copytable = TorrentXMLHandler.GetProcessedRows(); if (copytable.Rows.Count == 0) break; //Break out of while } else Thread.Sleep(150); } }