protected void BackgroundWorkerCalcStats_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { stopwatchCalcStats.Stop(); MyLog.AddElapsed(stopwatchCalcStats.Elapsed); VideoInfoStats videoInfoStatsResults = (VideoInfoStats)sender; if (videoInfoStatsResults != null) { CalcVideoInfoStats.Save(); } LoadVideos_Completed(sender, e); }
public static bool Load() { bool ret = false; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // MyLog.Add("Loading VideoItemStats"); string dataFile = MyFile.EnsureDataFile("stats", Config.settings.exportExt, "stats"); if (dataFile == null) { return(false); } // keep a backup of settings if (File.Exists(dataFile)) { videoInfoStats = new VideoInfoStats(); videoInfoStats = (VideoInfoStats)MyDeserialize.FromFile(Config.settings.exportFormat, dataFile, videoInfoStats); string fromFIle = "from " + dataFile.Replace(MyFile.exeDirectory, ""); FileInfo fileInfo = MyFile.FileInfo(dataFile); if (fileInfo != null) { fromFIle += " " + MyFile.FormatSize(fileInfo.Length); } MyLog.Add("Loaded VideoInfoStats " + fromFIle); if (videoInfoStats != null && videoInfoStats.year != null && videoInfoStats.year.Count() > 0) { ret = true; } } else { // MyLog.Add("No VideoInfoStats to load "); } stopWatch.Stop(); if (ret) { MyLog.AddElapsed(stopWatch.Elapsed); } return(ret); }
protected void BackgroundWorkerCalcStats_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { VideoInfoStats videoInfoStatsResults = new VideoInfoStats(); // First, handle the case where an exception was thrown. if (e.Error != null) { MyLog.Add(e.Error.ToString()); subFormProgress.Text(e.Error.Message); } else if (e.Cancelled) { // Next, handle the case where the user canceled the operation. // Note that due to a race condition in the DoWork event handler, the Cancelled // flag may not have been set, even though CancelAsync was called. subFormProgress.Text("Canceled"); } else { // Finally, handle the case where the operation succeeded. videoInfoStatsResults = (VideoInfoStats)e.Result; if (videoInfoStatsResults != null) { videoInfoStats = videoInfoStatsResults; } subFormProgress.Text("Ready"); subFormProgress.Value(0); } // bubble the event up var handler = backgroundWorkerCalcStats_RunWorkerCompleted; if (handler != null) { handler(videoInfoStatsResults, e); } }
public VideoInfoStats CalcVideoInfoStats(List <VideoInfo> videoInfos, DoWorkEventArgs doWorkEvent) { VideoInfoStats videoInfoStats = new VideoInfoStats(); if (videoInfos == null) { return(null); } // Abort the operation if the user has canceled. // Note that a call to CancelAsync may have set // CancellationPending to true just after the // last invocation of this method exits, so this // code will not have the opportunity to set the // DoWorkEventArgs.Cancel flag to true. This means // that RunWorkerCompletedEventArgs.Cancelled will // not be set to true in your RunWorkerCompleted // event handler. This is a race condition. if (backgroundWorker.CancellationPending) { doWorkEvent.Cancel = true; return(null); } // all the dictionary to custom list object is due to wanting to xml serialze data // see VideInfoStats.cs for more info .. Dictionary doesnt serialzie nicely Dictionary <string, int> statsS; Dictionary <int, int> statsI; Dictionary <decimal, int> statsD; // lists percentComplete = 0; backgroundWorker.ReportProgress(percentComplete, "Calc stats.."); statsS = videoInfos.Where(x => x.videoItem.actors != null).SelectMany(x => x.videoItem.actors).Where(s => s.name != null).GroupBy(s => s.name.Trim()).ToDictionary(g => g.Key, g => g.Count()); videoInfoStats.actor = new List <VideoInfoStatsQty <string, int> >(); foreach (KeyValuePair <string, int> stat in statsS) { videoInfoStats.actor.Add(new VideoInfoStatsQty <string, int>(stat.Key, stat.Value)); } percentComplete = 5; backgroundWorker.ReportProgress(percentComplete, "Calc stats.."); statsS = videoInfos.Where(x => x.videoItem.directors != null).SelectMany(x => x.videoItem.directors).Where(s => s.name != null).GroupBy(s => s.name.Trim()).ToDictionary(g => g.Key, g => g.Count()); videoInfoStats.director = new List <VideoInfoStatsQty <string, int> >(); foreach (KeyValuePair <string, int> stat in statsS) { videoInfoStats.director.Add(new VideoInfoStatsQty <string, int>(stat.Key, stat.Value)); } percentComplete = 10; backgroundWorker.ReportProgress(percentComplete, "Calc stats.."); statsS = videoInfos.Where(x => x.videoItem.genres != null).SelectMany(x => x.videoItem.genres).Where(s => s.name != null).GroupBy(s => s.name.Trim()).ToDictionary(g => g.Key, g => g.Count()); videoInfoStats.genre = new List <VideoInfoStatsQty <string, int> >(); foreach (KeyValuePair <string, int> stat in statsS) { videoInfoStats.genre.Add(new VideoInfoStatsQty <string, int>(stat.Key, stat.Value)); } percentComplete = 15; backgroundWorker.ReportProgress(percentComplete, "Calc stats.."); statsS = videoInfos.Where(x => x.videoItem.tags != null).SelectMany(x => x.videoItem.tags).Where(s => s.name != null).GroupBy(s => s.name.Trim()).ToDictionary(g => g.Key, g => g.Count()); videoInfoStats.tag = new List <VideoInfoStatsQty <string, int> >(); foreach (KeyValuePair <string, int> stat in statsS) { videoInfoStats.tag.Add(new VideoInfoStatsQty <string, int>(stat.Key, stat.Value)); } if (backgroundWorker.CancellationPending) { doWorkEvent.Cancel = true; return(null); } // string percentComplete = 20; backgroundWorker.ReportProgress(percentComplete, "Calc stats.."); statsS = videoInfos.Where(x => x.videoItem.mpaa != null).GroupBy(x => x.videoItem.mpaa).ToDictionary(x => x.Key, x => x.Count()); videoInfoStats.mpaa = new List <VideoInfoStatsQty <string, int> >(); foreach (KeyValuePair <string, int> stat in statsS) { videoInfoStats.mpaa.Add(new VideoInfoStatsQty <string, int>(stat.Key, stat.Value)); } percentComplete = 25; backgroundWorker.ReportProgress(percentComplete, "Calc stats.."); statsS = videoInfos.Where(x => x.videoItem.source != null).GroupBy(x => x.videoItem.source).ToDictionary(x => x.Key, x => x.Count()); videoInfoStats.source = new List <VideoInfoStatsQty <string, int> >(); foreach (KeyValuePair <string, int> stat in statsS) { videoInfoStats.source.Add(new VideoInfoStatsQty <string, int>(stat.Key, stat.Value)); } percentComplete = 30; backgroundWorker.ReportProgress(percentComplete, "Calc stats.."); statsS = videoInfos.Where(x => x.sourceAlias != null).GroupBy(x => x.sourceAlias).ToDictionary(x => x.Key, x => x.Count()); videoInfoStats.sourceAlias = new List <VideoInfoStatsQty <string, int> >(); foreach (KeyValuePair <string, int> stat in statsS) { videoInfoStats.sourceAlias.Add(new VideoInfoStatsQty <string, int>(stat.Key, stat.Value)); } percentComplete = 35; backgroundWorker.ReportProgress(percentComplete, "Calc stats.."); statsS = videoInfos.Where(x => x.videoItem.version != null).GroupBy(x => x.videoItem.version).ToDictionary(x => x.Key, x => x.Count()); videoInfoStats.version = new List <VideoInfoStatsQty <string, int> >(); foreach (KeyValuePair <string, int> stat in statsS) { videoInfoStats.version.Add(new VideoInfoStatsQty <string, int>(stat.Key, stat.Value)); } percentComplete = 40; backgroundWorker.ReportProgress(percentComplete, "Calc stats.."); statsS = videoInfos.Where(x => x.videoItem.watched != null).GroupBy(x => x.videoItem.watched).ToDictionary(x => x.Key, x => x.Count()); videoInfoStats.watched = new List <VideoInfoStatsQty <string, int> >(); foreach (KeyValuePair <string, int> stat in statsS) { videoInfoStats.watched.Add(new VideoInfoStatsQty <string, int>(stat.Key, stat.Value)); } //foreach (VideoInfo videoInfo in videoInfos) //{ // MyLog.Add("STATS : " + videoInfo.videoItem.searchTitle + " - " + videoInfo.videoItem.watched); //} if (backgroundWorker.CancellationPending) { doWorkEvent.Cancel = true; return(null); } // int, decimal percentComplete = 45; backgroundWorker.ReportProgress(percentComplete, "Calc stats.."); statsI = videoInfos.GroupBy(x => x.videoItem.encoding.height).ToDictionary(x => x.Key, x => x.Count()); videoInfoStats.height = new List <VideoInfoStatsQty <int, int> >(); foreach (KeyValuePair <int, int> stat in statsI) { videoInfoStats.height.Add(new VideoInfoStatsQty <int, int>(stat.Key, stat.Value)); } percentComplete = 45; backgroundWorker.ReportProgress(percentComplete, "Calc stats.."); statsD = videoInfos.GroupBy(x => x.videoItem.imdbRating).ToDictionary(x => x.Key, x => x.Count()); videoInfoStats.imdbRating = new List <VideoInfoStatsQty <decimal, int> >(); foreach (KeyValuePair <decimal, int> stat in statsD) { videoInfoStats.imdbRating.Add(new VideoInfoStatsQty <decimal, int>(stat.Key, stat.Value)); } percentComplete = 50; backgroundWorker.ReportProgress(percentComplete, "Calc stats.."); statsI = videoInfos.GroupBy(x => x.videoItem.playCount).ToDictionary(x => x.Key, x => x.Count()); videoInfoStats.playCount = new List <VideoInfoStatsQty <int, int> >(); foreach (KeyValuePair <int, int> stat in statsI) { videoInfoStats.playCount.Add(new VideoInfoStatsQty <int, int>(stat.Key, stat.Value)); } percentComplete = 55; backgroundWorker.ReportProgress(percentComplete, "Calc stats.."); statsI = videoInfos.GroupBy(x => x.videoItem.rating).ToDictionary(x => x.Key, x => x.Count()); videoInfoStats.rating = new List <VideoInfoStatsQty <int, int> >(); foreach (KeyValuePair <int, int> stat in statsI) { videoInfoStats.rating.Add(new VideoInfoStatsQty <int, int>(stat.Key, stat.Value)); } if (backgroundWorker.CancellationPending) { doWorkEvent.Cancel = true; return(null); } percentComplete = 60; backgroundWorker.ReportProgress(percentComplete, "Calc stats.."); statsI = videoInfos.GroupBy(x => x.videoItem.runtime).ToDictionary(x => x.Key, x => x.Count()); videoInfoStats.runtime = new List <VideoInfoStatsQty <int, int> >(); foreach (KeyValuePair <int, int> stat in statsI) { videoInfoStats.runtime.Add(new VideoInfoStatsQty <int, int>(stat.Key, stat.Value)); } percentComplete = 65; backgroundWorker.ReportProgress(percentComplete, "Calc stats.."); statsI = videoInfos.GroupBy(x => x.videoItem.encoding.width).ToDictionary(x => x.Key, x => x.Count()); videoInfoStats.width = new List <VideoInfoStatsQty <int, int> >(); foreach (KeyValuePair <int, int> stat in statsI) { videoInfoStats.width.Add(new VideoInfoStatsQty <int, int>(stat.Key, stat.Value)); } percentComplete = 65; backgroundWorker.ReportProgress(percentComplete, "Calc stats.."); statsI = videoInfos.GroupBy(x => x.videoItem.year).ToDictionary(x => x.Key, x => x.Count()); videoInfoStats.year = new List <VideoInfoStatsQty <int, int> >(); foreach (KeyValuePair <int, int> stat in statsI) { videoInfoStats.year.Add(new VideoInfoStatsQty <int, int>(stat.Key, stat.Value)); } percentComplete = 100; backgroundWorker.ReportProgress(percentComplete, "Completed stats"); // meh, so completed shows Thread.Sleep(500); return(videoInfoStats); }
/// <summary> /// add criteria manually /// </summary> /// <param name="criteriaType"></param> /// <param name="selectedTags"></param> private void AddCriteria(string criteriaType, string selectedTags) { // check if valid criteria type if (!criteriaTypes.ContainsKey(criteriaType)) { return; } // check if criteria already exits, exit if so string name = "userControlTagCloud_" + criteriaType; int index = 0; foreach (Control control in flowLayoutPanelCriteria.Controls) { if (control.Name == name) { return; } index++; } VideoInfoStats videoItemStats = CalcVideoInfoStats.GetStats(); if (videoItemStats == null) { return; } flowLayoutPanelCriteria.SuspendLayout(); MyTagCloud userControlTagCloud = new MyTagCloud(); userControlTagCloud.Height = 100; userControlTagCloud.Name = "userControlTagCloud_" + criteriaType; userControlTagCloud.SetTagType(criteriaTypes[criteriaType]); List <MyTagCloud.TagItem> tagItems = new List <MyTagCloud.TagItem> { }; switch (criteriaType) { case "genre": tagItems = GetTagItemsString(videoItemStats.genre); userControlTagCloud.Height = 70; break; case "height": tagItems = GetTagItemsNumeric(videoItemStats.height); userControlTagCloud.Height = 60; break; case "imdbRating": tagItems = GetTagItemsNumeric(videoItemStats.imdbRating); userControlTagCloud.Height = 60; break; case "mpaa": tagItems = GetTagItemsString(videoItemStats.mpaa); userControlTagCloud.Height = 70; break; case "playCount": tagItems = GetTagItemsNumeric(videoItemStats.playCount); userControlTagCloud.Height = 60; break; case "rating": tagItems = GetTagItemsNumeric(videoItemStats.rating); userControlTagCloud.Height = 60; break; case "source": tagItems = GetTagItemsString(videoItemStats.source); userControlTagCloud.Height = 60; break; case "sourceAlias": tagItems = GetTagItemsString(videoItemStats.sourceAlias); userControlTagCloud.Height = 60; break; case "tag": tagItems = GetTagItemsString(videoItemStats.tag); userControlTagCloud.Height = 105; break; case "version": tagItems = GetTagItemsString(videoItemStats.version); userControlTagCloud.Height = 60; break; case "watched": foreach (VideoInfoStatsQty <string, int> item in videoItemStats.watched) { string watched = VideoFileEnums.watched.GetTextByKey(item.Key); tagItems.Add(new MyTagCloud.TagItem(watched, item.Value)); } tagItems.Sort((x, y) => x.Name.CompareTo(y.Name) * -1); userControlTagCloud.Height = 35; break; case "width": tagItems = GetTagItemsNumeric(videoItemStats.width); userControlTagCloud.Height = 60; break; case "year": List <int> yearAdded = new List <int> { }; foreach (VideoInfoStatsQty <int, int> item in videoItemStats.year) { // MyLog.Add("add year: " + item.Key.ToString() + " " + item.Value.ToString()); tagItems.Add(new MyTagCloud.TagItem(item.Key.ToString(), item.Value)); yearAdded.Add(item.Key); } int paddingYears = 2; int currentYear = System.DateTime.UtcNow.Year + paddingYears; int minYear = 1920; for (int year = currentYear; year > minYear; year--) { if (yearAdded.Contains(year)) { continue; } tagItems.Add(new MyTagCloud.TagItem(year.ToString(), 0)); } tagItems.Sort((x, y) => x.Name.CompareTo(y.Name) * -1); userControlTagCloud.Height = 105; break; default: return; } if (tagItems.Count == 0) { // TODO btr way to handle no results tagItems.Add(new MyTagCloud.TagItem("No results", 0)); // return; } userControlTagCloud.AddItems(tagItems, selectedTags); flowLayoutPanelCriteria.Controls.Add(userControlTagCloud); buttonCriteria.Text = "Remove Criteria"; NormalizeCriteriaWidth(); flowLayoutPanelCriteria.ResumeLayout(); }