public void StartReoccuringTask()
        {
            if (_tokenSource != null && !_tokenSource.IsCancellationRequested)
            {
                // previous task was *NOT* cancelled so we just go home here
                Logger.Debug("Attempted to start a second task but that was supressed (misuse and such)");
                return;
            }

            Logger.Info("Starting Reoccuring task");
            _tokenSource = new CancellationTokenSource();
            _tokenSource.Token.ThrowIfCancellationRequested();
            Task.Run(async() =>
            {
                while (!_tokenSource.IsCancellationRequested)
                {
                    Logger.Debug("Attempting to download and insert player statistics");
                    CncGeneralInfo info = await GatherData();
                    if (info != null)
                    {
                        InsertData(info);
                    }

                    Thread.Sleep(1000 * 60);
                }
            }, _tokenSource.Token);
        }
        private void InsertData(CncGeneralInfo info)
        {
            _oldPlayerCount = _ra3Users.Count;
            lstView.Dispatcher.Invoke(() =>
            {
                var newEntries = info.Ra3.Users.Values.Except(_ra3Users);
                var oldEntries = _ra3Users.Except(info.Ra3.Users.Values);
                foreach (var newEntry in newEntries)
                {
                    AddSorted(_ra3Users, newEntry);
                }

                foreach (var o in oldEntries.ToList())
                {
                    _ra3Users.Remove(o);
                }
            });

            lstView.Dispatcher.Invoke(() =>
            {
                if (_oldPlayerCount != _ra3Users.Count)
                {
                    lblPlayers.Content = $"Players: {_ra3Users.Count}";
                }
            });
        }
        private async Task <CncGeneralInfo> GatherData()
        {
            try
            {
                WebRequest req = WebRequest.CreateHttp(Constants.CncOnlinePlayerInfo);

                var res = await req.GetResponseAsync();

                HttpWebResponse response = (HttpWebResponse)res;

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    Logger.Error($"ER.. Could not get a proper status. Status Code was ${response.StatusCode}");
                    return(null);
                }

                CncGeneralInfo info = new CncGeneralInfo();

                using (Stream streamResponse = response.GetResponseStream())
                    using (StreamReader sr =
                               new StreamReader(streamResponse ?? throw new InvalidOperationException("streamResponse was null")))
                    {
                        string body = await sr.ReadToEndAsync();

                        info = JsonConvert.DeserializeObject <CncGeneralInfo>(body);
                    }

                return(info);
            }
            catch (SecurityException err)
            {
                Logger.Error($"ER.. Could not connect to Server due to permission issues. err: {err.Message}");
            }
            catch (ProtocolViolationException err)
            {
                Logger.Error($"ER.. Could not get any response stream. err: {err.Message}");
            }
            catch (InvalidOperationException err)
            {
                Logger.Error($"ER.. Seems like the responsestream was null for some reason. err: {err.Message}");
            }
            catch (Exception err)
            {
                Logger.Fatal($"ER.. Seems like something unexpected happened. err: {err.Message}");
            }

            return(null);
        }