コード例 #1
0
ファイル: TabsVM.cs プロジェクト: vildar82/AcadLib
        private void LoadHistory()
        {
            Logger.Info("RestoreTabs TabsVM LoadHistory start");
            var cache = HistoryModel.LoadHistoryCache();

            if (cache.Any())
            {
                Task.Run(() =>
                {
                    var tabs = cache.Select(s => GetTab(s.File, false, s.Start)).ToList();
                    dispatcher.Invoke(() =>
                    {
                        try
                        {
                            tabs.ForEach(t => historySrc.Add(t));
                        }
                        catch (Exception ex)
                        {
                            Logger.Error(ex, "RestoreTabs TabsVM dispatcher.Invoke History adds from cache.");
                        }
                    });
                });
            }

            Task.Run(() =>
            {
                var dbItems = cache.Any()
                    ? new DbHistory().LoadHistoryFiles(cache.Max(m => m.Start)).ToList()
                    : new DbHistory().LoadHistoryFiles().ToList();
                Logger.Info("RestoreTabs TabsVM Load dbItems.");
                var tabs = dbItems.Select(s => GetTab(s.DocPath, false, s.Start)).ToList();
                Task.Delay(TimeSpan.FromMilliseconds(300)).Wait();
                dispatcher.Invoke(() =>
                {
                    try
                    {
                        tabs.ForEach(t => historySrc.Add(t));
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex, "RestoreTabs TabsVM dispatcher.Invoke History");
                    }
                });

                var removeTabs = historySrc.Items.GroupBy(g => g.File).SelectMany(s => s.OrderByDescending(o => o.Start).Skip(1));
                Task.Delay(TimeSpan.FromMilliseconds(300)).Wait();
                foreach (var tab in removeTabs)
                {
                    dispatcher.Invoke(() =>
                    {
                        try
                        {
                            historySrc.Remove(tab);
                        }
                        catch
                        {
                        }
                    });
                }
            });

            // Загрузка из базы и сохранение в кеш
            Task.Run(() =>
            {
                if (!cache.Any() || HistoryModel.NeedUpdateCashe())
                {
                    var dbItems = new DbHistory().LoadHistoryFiles().ToList();
                    var tabs    = dbItems.Select(s => new HistoryTab {
                        File = s.DocPath, Start = s.Start
                    })
                                  .GroupBy(g => g.File).Select(s => s.OrderByDescending(o => o.Start).FirstOrDefault()).ToList();
                    HistoryModel.SaveHistoryCache(tabs);
                }
            });
        }