<commit_before>﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using DesktopWidgets.Helpers;
using DesktopWidgets.WidgetBase;
using DesktopWidgets.WidgetBase.ViewModel;

namespace DesktopWidgets.Widgets.LatencyMonitor
{
    public class ViewModel : WidgetViewModelBase
    {
        private long _lastDownloadUsage;
        private long _lastLatency;
        private long _lastUploadUsage;
        private bool _scanLatency;

        public ViewModel(WidgetId id) : base(id)
        {
            Settings = id.GetSettings() as Settings;
            if (Settings == null)
            {
                return;
            }
            LatencyHistory = new ObservableCollection<TextBlock>();
            _lastLatency = -1;
            _lastDownloadUsage = GetDownloadedBytes();
            _lastUploadUsage = GetUploadedBytes();
            _scanLatency = true;
            StartLatencyReader();
        }

        public Settings Settings { get; }

        public ObservableCollection<TextBlock> LatencyHistory { get; }

        private void StartLatencyReader()
        {
            new Thread(() =>
            {
                Thread.CurrentThread.IsBackground = true;
                using (var ping = new Ping())
                {
                    while (_scanLatency)
                    {
                        try
                        {
                            var reply = GetLatency(ping);
                            if (reply != null)
                            {
                                var downloadedBytes = GetDownloadedBytes();
                                var uploadedBytes = GetUploadedBytes();
                                Application.Current.Dispatcher.Invoke(
                                    DispatcherPriority.Background,
                                    new Action(() =>
                                    {
                                        try
                                        {
                                            var latencyTextBlock = new TextBlock
                                            {
                                                Text =
                                                    GetLatencyText(reply, downloadedBytes - _lastDownloadUsage,
                                                        uploadedBytes - _lastUploadUsage),
                                                Foreground = new SolidColorBrush(GetLatencyBrush(reply))
                                            };
                                            while (LatencyHistory.Count + 1 > Settings.MaxHistory)
                                            {
                                                LatencyHistory.RemoveAt(0);
                                            }
                                            LatencyHistory.Add(latencyTextBlock);
                                        }
                                        catch
                                        {
                                            // ignored
                                        }
                                    }));
                                _lastLatency = reply.RoundtripTime;
                                _lastDownloadUsage = GetDownloadedBytes();
                                _lastUploadUsage = GetUploadedBytes();
                            }
                        }
                        catch
                        {
                            // ignored
                        }
                        Thread.Sleep(Settings.PingInterval);
                    }
                }
            }).Start();
        }<commit_msg>Don't wait for UI update to scan latency in "Latency Monitor"<commit_after>﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using DesktopWidgets.Helpers;
using DesktopWidgets.WidgetBase;
using DesktopWidgets.WidgetBase.ViewModel;

namespace DesktopWidgets.Widgets.LatencyMonitor
{
    public class ViewModel : WidgetViewModelBase
    {
        private long _lastDownloadUsage;
        private long _lastLatency;
        private long _lastUploadUsage;
        private bool _scanLatency;

        public ViewModel(WidgetId id) : base(id)
        {
            Settings = id.GetSettings() as Settings;
            if (Settings == null)
            {
                return;
            }
            LatencyHistory = new ObservableCollection<TextBlock>();
            _lastLatency = -1;
            _lastDownloadUsage = GetDownloadedBytes();
            _lastUploadUsage = GetUploadedBytes();
            _scanLatency = true;
            StartLatencyReader();
        }

        public Settings Settings { get; }

        public ObservableCollection<TextBlock> LatencyHistory { get; }

        private void StartLatencyReader()
        {
            new Thread(() =>
            {
                Thread.CurrentThread.IsBackground = true;
                using (var ping = new Ping())
                {
                    while (_scanLatency)
                    {
                        try
                        {
                            var reply = GetLatency(ping);
                            if (reply != null)
                            {
                                var downloadedBytes = GetDownloadedBytes();
                                var uploadedBytes = GetUploadedBytes();
                                var downloadDifference = downloadedBytes - _lastDownloadUsage;
                                var uploadDifference = uploadedBytes - _lastUploadUsage;
                                _lastLatency = reply.RoundtripTime;
                                _lastDownloadUsage = GetDownloadedBytes();
                                _lastUploadUsage = GetUploadedBytes();
                                Application.Current.Dispatcher.BeginInvoke(
                                    DispatcherPriority.Background,
                                    new Action(() =>
                                    {
                                        try
                                        {
                                            var latencyTextBlock = new TextBlock
                                            {
                                                Text =
                                                    GetLatencyText(reply, downloadDifference, uploadDifference),
                                                Foreground = new SolidColorBrush(GetLatencyBrush(reply))
                                            };
                                            while (LatencyHistory.Count + 1 > Settings.MaxHistory)
                                            {
                                                LatencyHistory.RemoveAt(0);
                                            }
                                            LatencyHistory.Add(latencyTextBlock);
                                        }
                                        catch
                                        {
                                            // ignored
                                        }
                                    }));
                            }
                        }
                        catch
                        {
                            // ignored
                        }
                        Thread.Sleep(Settings.PingInterval);
                    }
                }
            }).Start();
        }
{'fraction_non_alphanumeric': 0.05653665353830517, 'fraction_numerical': 0.000762291957819845, 'mean_word_length': 0.8319757970677216, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 96, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '248539', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1363, 'n_words': 294}