private void BackgroundWorkerThread() { // check first and download items string desigmapfile = Path.Combine(EDDOptions.Instance.AppDataDirectory, "bodydesignations.csv"); if (!File.Exists(desigmapfile)) { desigmapfile = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "bodydesignations.csv"); } BodyDesignations.LoadBodyDesignationMap(desigmapfile); Debug.WriteLine(BaseUtils.AppTicks.TickCountLap() + " Check systems"); ReportSyncProgress(""); bool checkGithub = EDDOptions.Instance.CheckGithubFiles; if (checkGithub) // not normal in debug, due to git hub choking { DateTime lastdownloadtime = UserDatabase.Instance.GetSettingDate("DownloadFilesLastTime", DateTime.MinValue); if (DateTime.UtcNow - lastdownloadtime >= new TimeSpan(24, 0, 0)) // only update once per day { // Expedition data DownloadExpeditions(() => PendingClose); // and Help files DownloadHelp(() => PendingClose); UserDatabase.Instance.PutSettingDate("DownloadFilesLastTime", DateTime.UtcNow); } } // if we have a gmo file, but its out of date, refresh it for next time, do it in background thread since not critical. string gmofile = Path.Combine(EDDOptions.Instance.AppDataDirectory, "galacticmapping.json"); if (!EDDOptions.Instance.NoSystemsLoad && File.Exists(gmofile) && DateTime.UtcNow.Subtract(SystemsDatabase.Instance.GetEDSMGalMapLast()).TotalDays > 14) { LogLine("Get galactic mapping from EDSM.".T(EDTx.EDDiscoveryController_EDSM)); if (EDSMClass.DownloadGMOFileFromEDSM(gmofile)) { SystemsDatabase.Instance.SetEDSMGalMapLast(DateTime.UtcNow); } } Debug.WriteLine(BaseUtils.AppTicks.TickCountLap() + " Check systems complete"); SystemNoteClass.GetAllSystemNotes(); LogLine("Loaded Notes, Bookmarks and Galactic mapping.".T(EDTx.EDDiscoveryController_LN)); if (!EDDOptions.Instance.NoLoad) // here in this thread, we do a refresh of history. { LogLine("Reading travel history".T(EDTx.EDDiscoveryController_RTH)); if (EDDOptions.Instance.Commander != null) { EDCommander switchto = EDCommander.GetCommander(EDDOptions.Instance.Commander); if (switchto != null) { EDCommander.CurrentCmdrID = switchto.Id; } } DoRefreshHistory(new RefreshWorkerArgs { CurrentCommander = EDCommander.CurrentCmdrID }); // load history the first time in this thread } CheckForSync(); // see if any EDSM sync is needed - this just sets some variables up System.Diagnostics.Debug.WriteLine("Background worker setting up refresh worker"); backgroundRefreshWorker = new Thread(BackgroundHistoryRefreshWorkerThread) { Name = "Background Refresh Worker", IsBackground = true }; backgroundRefreshWorker.Start(); // start the refresh worker, another thread which does subsequenct (not the primary one) refresh work in the background.. try { if (!EDDOptions.Instance.NoSystemsLoad) { DoPerformSync(); // this is done after the initial history load.. } while (!PendingClose) { int wh = WaitHandle.WaitAny(new WaitHandle[] { closeRequested, resyncRequestedEvent }); System.Diagnostics.Debug.WriteLine("Background worker kicked by " + wh); if (PendingClose) { break; } if (wh == 1) { if (!EDDOptions.Instance.NoSystemsLoad && EDDConfig.Instance.EDSMDownload) // if no system off, and EDSM download on { DoPerformSync(); } } } } catch (OperationCanceledException) { } backgroundRefreshWorker.Join(); // this should terminate due to closeRequested.. System.Diagnostics.Debug.WriteLine("BW Refresh joined closing down background worker"); // Now we have been ordered to close down, so go thru the process closeRequested.WaitOne(); InvokeAsyncOnUiThread(() => { System.Diagnostics.Debug.WriteLine("Call Final close"); OnFinalClose?.Invoke(); }); }