void WriteLineConsoleCheck() { String OnDate = DateTime.Now.ToString("yyyy-MM-dd"); String LogFilename = Module.TravoxSentinel + "log\\" + OnDate + Module.File_Log; String ErrorFilename = Module.TravoxSentinel + "error" + Module.File_Log; if (!File.Exists(LogFilename)) { this.WriteLineConsoleDispose(); if (!Directory.Exists(Path.GetDirectoryName(LogFilename))) Directory.CreateDirectory(Path.GetDirectoryName(LogFilename)); using (logWrite = new ConsoleWriter(LogFilename, FileMode.Create)) { logWrite.WriteEvent += txtLogMessage_WriteEvent; logWrite.WriteLineEvent += txtLogMessage_WriteLineEvent; Console.SetOut(logWrite); } } if (!File.Exists(ErrorFilename)) { StreamWriter errorWrite = new StreamWriter(new FileStream(ErrorFilename, FileMode.Create)); errorWrite.AutoFlush = true; Console.SetError(errorWrite); } }
private void WorkCrawlerCollection(object sender, DoWorkEventArgs e) { Int32 ThreadProgress = 0; logWrite = null; logWatch = null; BackgroundWorker init = sender as BackgroundWorker; DB database; DataTable db_customer = new DataTable(); try { // if(!App.DebugMode) this.LogRecheck(); // Sentinel Crawler init.ReportProgress(0, StateTravox.InitDatabase); database = new DB("travox_global"); String db = "SELECT id, code, database_name, [description] FROM site_customer "; db += "WHERE[status] = 'ACTIVE' AND ISNULL(database_name,'') <> '' AND sentinel = 'Y'"; db_customer = database.GetTable(db); DBTotal = db_customer.Rows.Count; // Initinalize Database Travox } catch (Exception ex) { MessageBox.Show(ex.Message, "Travox Sentinel " + App.PublishVersion.ToString(), MessageBoxButton.OK, MessageBoxImage.Exclamation); e.Cancel = true; return; } HandlerItems[] TaskAgrs = new HandlerItems[DBTotal]; init.ReportProgress(0, StateTravox.LoadDatabase); CtrlTotal = this.CrawlerTravoxDBInitialize().Count; for (Int32 db = 0; db < DBTotal; db++) { TaskAgrs[db] = new HandlerItems(); TaskAgrs[db].Items = new List<HandlerCrawler>(); Thread.Sleep(10); init.ReportProgress(ThreadProgress++, StateTravox.LoadDatabase); CrawlerDatabase = "Initialize " + db_customer.Rows[db]["description"].ToString(); foreach (Controller item in this.CrawlerTravoxDBInitialize()) { if (!item.OnceTime || db_customer.Rows[db]["database_name"].ToString() == item.DBName) { HandlerCrawler CreatedCrawler = new HandlerCrawler(); CreatedCrawler.Crawler = item; CreatedCrawler.Crawler.State = new HandlerState(); CreatedCrawler.Crawler.State.ThreadId = Idx; CreatedCrawler.Crawler.State.DatabaseName = db_customer.Rows[db]["database_name"].ToString(); CreatedCrawler.Crawler.State.CompanyName = db_customer.Rows[db]["description"].ToString(); CreatedCrawler.Crawler.State.CompanyCode = db_customer.Rows[db]["code"].ToString(); CreatedCrawler.Crawler.State.CompanyID = db_customer.Rows[db]["id"].ToString(); CreatedCrawler.Task = Task.Factory.StartNew((object state) => { }, null); TaskAgrs[db].Items.Add(CreatedCrawler); this.Task_HandlerControl((object)CreatedCrawler.Crawler); Idx++; } } } init.ReportProgress(ThreadProgress++, StateTravox.LoadDatabase); Thread.Sleep(100); //100 init.ReportProgress(0, StateTravox.InitSuccess); db_customer.Clear(); db_customer = null; do { for (Int32 db = 0; db < DBTotal; db++) { if (!CrawlerRunning) break; for (Int32 i = 0; i < TaskAgrs[db].Items.Count; i++) { if (!CrawlerRunning || !TaskAgrs[db].Items[i].Crawler.SetEnabled) break; switch (TaskAgrs[db].Items[i].Task.Status) { case TaskStatus.Canceled: case TaskStatus.Faulted: case TaskStatus.RanToCompletion: TaskAgrs[db].Items[i].Task.Dispose(); TaskAgrs[db].Items[i].Crawler.IsStarted = true; TaskAgrs[db].Items[i].Task = Task.Factory.StartNew(Task_HandlerControl, TaskAgrs[db].Items[i].Crawler); break; } } } this.WriteLineConsoleCheck(); // Thread Sleep Manual. init.ReportProgress(0, StateTravox.OnStatus); Stopwatch SleepTime = new Stopwatch(); TimeSpan _TimeInterval = new TimeSpan(0, 0, 3); SleepTime.Start(); do { Thread.Sleep(128); } while (CrawlerRunning && SleepTime.ElapsedMilliseconds < _TimeInterval.TotalMilliseconds); SleepTime.Stop(); // Thread Sleep Manual. } while (CrawlerRunning); App.ServerConnected = false; App.WebCrawlerConnected = false; CrawlerDatabase = null; init.ReportProgress(0, StateTravox.InitShutdown); Int32 DBStoped = 0; Int32 DBStopedMax = 0; do { DBStoped = 0; for (Int32 db = 0; db < DBTotal; db++) { Int32 CrawlerTotal = TaskAgrs[db].Items.Count, CrawlerStoping = 0; for (Int32 i = 0; i < TaskAgrs[db].Items.Count; i++) { switch (TaskAgrs[db].Items[i].Task.Status) { case TaskStatus.Canceled: case TaskStatus.Faulted: case TaskStatus.RanToCompletion: CrawlerStoping++; if (!TaskAgrs[db].Items[i].Crawler.IsStoped) TaskAgrs[db].Items[i].Crawler.Stop(); break; } } if (CrawlerStoping == CrawlerTotal) DBStoped++; if (DBStopedMax < DBStoped) DBStopedMax = DBStoped; Thread.Sleep(10); init.ReportProgress(DBStopedMax, StateTravox.InitShutdown); } } while (DBStoped != DBTotal); }