// I think we rework so we have a SyncBackgroundworker, and a refresh background worker.
// loading of history is done on refresh one..


        private void BackgroundWorkerThread()
        {
            readyForInitialLoad.WaitOne();      // wait for shown in form

            // check first and download items

            StarScan.LoadBodyDesignationMap();

            Debug.WriteLine(BaseUtils.AppTicks.TickCountLap() + " Check systems");
            ReportSyncProgress("");

            bool checkGithub = EDDOptions.Instance.CheckGithubFiles;

            if (checkGithub)      // not normall in debug, due to git hub chokeing
            {
                // Async load of maps in another thread
                DownloadMaps(() => PendingClose);

                // and Expedition data
                DownloadExpeditions(() => PendingClose);

                // and Exploration data
                DownloadExploration(() => PendingClose);
            }

            if (!EDDOptions.Instance.NoSystemsLoad)
            {
                // New Galmap load - it was not doing a refresh if EDSM sync kept on happening. Now has its own timer

                DateTime galmaptime = SQLiteConnectionSystem.GetSettingDate("EDSMGalMapLast", DateTime.MinValue); // Latest time from RW file.

                if (DateTime.Now.Subtract(galmaptime).TotalDays > 14 || !galacticMapping.GalMapFilePresent())     // Over 14 days do a sync from EDSM for galmap
                {
                    LogLine("Get galactic mapping from EDSM.".Tx(this, "EDSM"));
                    if (galacticMapping.DownloadFromEDSM())
                    {
                        SQLiteConnectionSystem.PutSettingDate("EDSMGalMapLast", DateTime.UtcNow);
                    }
                }

                Debug.WriteLine(BaseUtils.AppTicks.TickCountLap() + " Check systems complete");
            }

            galacticMapping.ParseData();                            // at this point, gal map data has been uploaded - get it into memory
            SystemCache.AddToAutoCompleteList(galacticMapping.GetGMONames());
            SystemNoteClass.GetAllSystemNotes();

            LogLine("Loaded Notes, Bookmarks and Galactic mapping.".Tx(this, "LN"));

            if (PendingClose)
            {
                return;
            }

            if (EliteDangerousCore.EDDN.EDDNClass.CheckforEDMC()) // EDMC is running
            {
                if (EDCommander.Current.SyncToEddn)               // Both EDD and EDMC should not sync to EDDN.
                {
                    LogLineHighlight("EDDiscovery and EDMarketConnector should not both sync to EDDN. Stop EDMC or uncheck 'send to EDDN' in settings tab!".Tx(this, "EDMC"));
                }
            }

            if (!EDDOptions.Instance.NoLoad)        // here in this thread, we do a refresh of history.
            {
                LogLine("Reading travel history".Tx(this, "RTH"));
                DoRefreshHistory(new RefreshWorkerArgs {
                    CurrentCommander = EDCommander.CurrentCmdrID
                });                                                                                             // kick the background refresh worker thread into action
            }

            if (PendingClose)
            {
                return;
            }

            CheckForSync();     // see if any EDSM/EDDB sync is needed

            if (PendingClose)
            {
                return;
            }

            // Now stay in loop services stuff

            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 && EDDConfig.Instance.EDSMEDDBDownload) // if no system off, and EDSM download on
                {
                    DoPerformSync();                                                           // this is done after the initial history load..
                }
                while (!PendingClose)
                {
                    int wh = WaitHandle.WaitAny(new WaitHandle[] { closeRequested, resyncRequestedEvent });

                    if (PendingClose)
                    {
                        break;
                    }

                    switch (wh)
                    {
                    case 0:      // Close Requested
                        break;

                    case 1:                                                                            // Resync Requested
                        if (!EDDOptions.Instance.NoSystemsLoad && EDDConfig.Instance.EDSMEDDBDownload) // if no system off, and EDSM download on
                        {
                            DoPerformSync();
                        }
                        break;
                    }
                }
            }
            catch (OperationCanceledException)
            {
            }

            backgroundRefreshWorker.Join();

            // Now we have been ordered to close down, so go thru the process

            closeRequested.WaitOne();

            OnBgSafeClose?.Invoke();
            ReadyForFinalClose = true;
            InvokeAsyncOnUiThread(() =>
            {
                OnFinalClose?.Invoke();
            });
        }