/// <summary> /// Does query in background. /// </summary> /// <param name="state">The state.</param> /// <exception cref="NotImplementedException"></exception> private void DoQueryInBackground(object state) { do { if (!Monitor.TryEnter(this)) { return; } try { this.cache.Clear(); this.view.Clear(); var maxThreads = Settings.Default.MaxThreads > 0 ? Settings.Default.MaxThreads : Environment.ProcessorCount; var queue = new QueryQueue(this.GetType().Name, maxThreads); var dates = Enumerable.Range(7, 30).Select(i => DateTime.Now.Date.AddDays(i)).ToList(); this.view.ShowProgress("Report Earning Calendar", "Querying..."); this.view.ShowProgress(0, dates.Count); queue.Start(this.QueryCalendar, dates); this.view.ShowProgress("Report Earning Calendar", $"Retrieved earning reports between [{dates.First().ToShortDateString()} - {dates.Last().ToShortDateString()}]"); } catch (Exception ex) { var error = $"Failed to get calendar from {this.db.GetType().Name}: {ex}"; this.view.Notify(error); } finally { Monitor.Exit(this); } this.cache.Clear(); Thread.Sleep(Convert.ToInt32(TimeSpan.FromHours(1).TotalMilliseconds)); } while (true); }