From: sr55 Date: Thu, 7 Mar 2013 22:11:11 +0000 (+0000) Subject: WinGui: Some Threading, Performance and Log window fixes. X-Git-Tag: 0.9.9~83 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0058e66974b545911342f72078469653384b509d;p=handbrake WinGui: Some Threading, Performance and Log window fixes. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@5308 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- diff --git a/win/CS/HandBrake.ApplicationServices/Isolation/IsolatedEncodeService.cs b/win/CS/HandBrake.ApplicationServices/Isolation/IsolatedEncodeService.cs index 3ddae9b1f..31c1d8cec 100644 --- a/win/CS/HandBrake.ApplicationServices/Isolation/IsolatedEncodeService.cs +++ b/win/CS/HandBrake.ApplicationServices/Isolation/IsolatedEncodeService.cs @@ -81,6 +81,17 @@ namespace HandBrake.ApplicationServices.Isolation } } + /// + /// Gets the log index. + /// + public int LogIndex + { + get + { + return -1; + } + } + /// /// Gets a value indicating whether IsEncoding. /// diff --git a/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs b/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs index 866edb794..555b004f2 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/Base/EncodeBase.cs @@ -15,8 +15,6 @@ namespace HandBrake.ApplicationServices.Services.Base using System.Text; using System.Text.RegularExpressions; - using Caliburn.Micro; - using HandBrake.ApplicationServices.EventArgs; using HandBrake.ApplicationServices.Exceptions; using HandBrake.ApplicationServices.Model; @@ -40,11 +38,6 @@ namespace HandBrake.ApplicationServices.Services.Base /// private readonly IUserSettingService userSettingService; - /// - /// Windows 7 API Pack wrapper - /// - private readonly Win7 windowsSeven = new Win7(); - /// /// The Log File Header /// @@ -76,6 +69,8 @@ namespace HandBrake.ApplicationServices.Services.Base GeneralUtilities.CreateCliLogHeader( userSettingService.GetUserSetting(ASUserSettingConstants.HandBrakeVersion), userSettingService.GetUserSetting(ASUserSettingConstants.HandBrakeBuild)); + + this.LogIndex = 0; } #region Events @@ -119,24 +114,18 @@ namespace HandBrake.ApplicationServices.Services.Base } /// - /// Gets LogBuffer. + /// Gets the log index. /// - public StringBuilder LogBuffer - { - get - { - return this.logBuffer; - } - } + public int LogIndex { get; private set; } /// - /// Gets WindowsSeven. + /// Gets LogBuffer. /// - public Win7 WindowsSeven + public StringBuilder LogBuffer { get { - return this.windowsSeven; + return this.logBuffer; } } @@ -152,15 +141,11 @@ namespace HandBrake.ApplicationServices.Services.Base /// public void InvokeEncodeStatusChanged(EncodeProgressEventArgs e) { - Execute.OnUIThread( - () => - { - EncodeProgessStatus handler = this.EncodeStatusChanged; - if (handler != null) - { - handler(this, e); - } - }); + EncodeProgessStatus handler = this.EncodeStatusChanged; + if (handler != null) + { + handler(this, e); + } } /// @@ -171,15 +156,13 @@ namespace HandBrake.ApplicationServices.Services.Base /// public void InvokeEncodeCompleted(EncodeCompletedEventArgs e) { - Execute.OnUIThread( - () => - { - EncodeCompletedStatus handler = this.EncodeCompleted; - if (handler != null) - { - handler(this, e); - } - }); + EncodeCompletedStatus handler = this.EncodeCompleted; + if (handler != null) + { + handler(this, e); + } + + this.LogIndex = 0; // Reset } /// @@ -190,14 +173,11 @@ namespace HandBrake.ApplicationServices.Services.Base /// public void InvokeEncodeStarted(EventArgs e) { - Execute.OnUIThread(() => - { - EventHandler handler = this.EncodeStarted; - if (handler != null) - { - handler(this, e); - } - }); + EventHandler handler = this.EncodeStarted; + if (handler != null) + { + handler(this, e); + } } #endregion @@ -261,7 +241,6 @@ namespace HandBrake.ApplicationServices.Services.Base } } - /// /// Pase the CLI status output (from standard output) /// @@ -393,6 +372,8 @@ namespace HandBrake.ApplicationServices.Services.Base { try { + this.LogIndex = this.LogIndex + 1; + lock (this.LogBuffer) { this.LogBuffer.AppendLine(message); diff --git a/win/CS/HandBrake.ApplicationServices/Services/Encode.cs b/win/CS/HandBrake.ApplicationServices/Services/Encode.cs index 577067050..b4f572521 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/Encode.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/Encode.cs @@ -11,7 +11,6 @@ namespace HandBrake.ApplicationServices.Services { using System; using System.Diagnostics; - using System.Globalization; using System.IO; using System.Windows.Forms; @@ -66,7 +65,7 @@ namespace HandBrake.ApplicationServices.Services public Encode(IUserSettingService userSettingService) : base(userSettingService) { - this.userSettingService = userSettingService; + this.userSettingService = userSettingService; } #region Properties @@ -135,7 +134,7 @@ namespace HandBrake.ApplicationServices.Services userSettingService.GetUserSetting(ASUserSettingConstants.PreviewScanCount), userSettingService.GetUserSetting(ASUserSettingConstants.Verbosity), userSettingService.GetUserSetting(ASUserSettingConstants.DisableLibDvdNav)) - : QueryGeneratorUtility.GenerateQuery(new EncodeTask(this.currentTask.Task), + : QueryGeneratorUtility.GenerateQuery(new EncodeTask(this.currentTask.Task), userSettingService.GetUserSetting(ASUserSettingConstants.PreviewScanCount), userSettingService.GetUserSetting(ASUserSettingConstants.Verbosity), userSettingService.GetUserSetting(ASUserSettingConstants.DisableLibDvdNav)); @@ -263,21 +262,16 @@ namespace HandBrake.ApplicationServices.Services } Execute.OnUIThread(() => + { + if (this.userSettingService.GetUserSetting(ASUserSettingConstants.PreventSleep)) { - if (this.WindowsSeven.IsWindowsSeven) - { - this.WindowsSeven.SetTaskBarProgressToNoProgress(); - } - - if (this.userSettingService.GetUserSetting(ASUserSettingConstants.PreventSleep)) - { - Win32.AllowSleep(); - } + Win32.AllowSleep(); + } + }); - this.currentTask.Status = QueueItemStatus.Completed; - this.IsEncoding = false; - this.InvokeEncodeCompleted(new EncodeCompletedEventArgs(true, null, string.Empty)); - }); + this.currentTask.Status = QueueItemStatus.Completed; + this.IsEncoding = false; + this.InvokeEncodeCompleted(new EncodeCompletedEventArgs(true, null, string.Empty)); } /// @@ -296,14 +290,14 @@ namespace HandBrake.ApplicationServices.Services { if (!String.IsNullOrEmpty(e.Data)) { - if (initShutdown && this.LogBuffer.Length < 25000000) + if (initShutdown && this.LogBuffer.Length < 25000000) { initShutdown = false; // Reset this flag. } if (this.LogBuffer.Length > 25000000 && !initShutdown) // Approx 23.8MB and make sure it's only printed once { - this.ProcessLogMessage("ERROR: Initiating automatic shutdown of encode process. The size of the log file inidcates that there is an error! "); + this.ProcessLogMessage("ERROR: Initiating automatic shutdown of encode process. The size of the log file indicates that there is an error! "); initShutdown = true; this.Stop(); } @@ -328,28 +322,14 @@ namespace HandBrake.ApplicationServices.Services EncodeProgressEventArgs eventArgs = this.ReadEncodeStatus(e.Data, this.startTime); if (eventArgs != null) { - Execute.OnUIThread( - () => - { - if (!this.IsEncoding) - { - // We can get events out of order since the CLI progress is monitored on a background thread. - // So make sure we don't send a status update after an encode complete event. - return; - } - - this.InvokeEncodeStatusChanged(eventArgs); - - if (this.WindowsSeven.IsWindowsSeven) - { - int percent; - int.TryParse( - Math.Round(eventArgs.PercentComplete).ToString(CultureInfo.InvariantCulture), - out percent); - - this.WindowsSeven.SetTaskBarProgress(percent); - } - }); + if (!this.IsEncoding) + { + // We can get events out of order since the CLI progress is monitored on a background thread. + // So make sure we don't send a status update after an encode complete event. + return; + } + + this.InvokeEncodeStatusChanged(eventArgs); } } } diff --git a/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IEncode.cs b/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IEncode.cs index 915d66788..fbb59ec2b 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IEncode.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/Interfaces/IEncode.cs @@ -66,6 +66,11 @@ namespace HandBrake.ApplicationServices.Services.Interfaces /// string ActivityLog { get; } + /// + /// Gets the log index. The current log row counter. + /// + int LogIndex { get; } + /// /// Start with a LibHb EncodeJob Object /// diff --git a/win/CS/HandBrake.ApplicationServices/Services/LibEncode.cs b/win/CS/HandBrake.ApplicationServices/Services/LibEncode.cs index 0d1998a59..33453dad8 100644 --- a/win/CS/HandBrake.ApplicationServices/Services/LibEncode.cs +++ b/win/CS/HandBrake.ApplicationServices/Services/LibEncode.cs @@ -254,14 +254,6 @@ namespace HandBrake.ApplicationServices.Services }; this.InvokeEncodeStatusChanged(args); - - if (this.WindowsSeven.IsWindowsSeven) - { - int percent; - int.TryParse(Math.Round(e.FractionComplete).ToString(CultureInfo.InvariantCulture), out percent); - - this.WindowsSeven.SetTaskBarProgress(percent); - } } /// @@ -282,11 +274,6 @@ namespace HandBrake.ApplicationServices.Services ? new EncodeCompletedEventArgs(false, null, string.Empty) : new EncodeCompletedEventArgs(true, null, string.Empty)); - if (this.WindowsSeven.IsWindowsSeven) - { - this.WindowsSeven.SetTaskBarProgressToNoProgress(); - } - if (this.userSettingService.GetUserSetting(ASUserSettingConstants.PreventSleep)) { Win32.AllowSleep(); diff --git a/win/CS/HandBrakeWPF/Services/EncodeServiceWrapper.cs b/win/CS/HandBrakeWPF/Services/EncodeServiceWrapper.cs index 8f424b65a..2dd1814ee 100644 --- a/win/CS/HandBrakeWPF/Services/EncodeServiceWrapper.cs +++ b/win/CS/HandBrakeWPF/Services/EncodeServiceWrapper.cs @@ -126,6 +126,17 @@ namespace HandBrakeWPF.Services } } + /// + /// Gets the log index. + /// + public int LogIndex + { + get + { + return this.encodeService.LogIndex; + } + } + /// /// Gets a value indicating whether IsEncoding. /// diff --git a/win/CS/HandBrakeWPF/ViewModels/Interfaces/ILogViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/Interfaces/ILogViewModel.cs index 6aa1020d4..6c7a7a2c8 100644 --- a/win/CS/HandBrakeWPF/ViewModels/Interfaces/ILogViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/Interfaces/ILogViewModel.cs @@ -14,5 +14,9 @@ namespace HandBrakeWPF.ViewModels.Interfaces /// public interface ILogViewModel { + /// + /// Gets or sets the selected tab. + /// + int SelectedTab { get; set; } } } \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/ViewModels/LogViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/LogViewModel.cs index 5bf95d22f..ad76a56b2 100644 --- a/win/CS/HandBrakeWPF/ViewModels/LogViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/LogViewModel.cs @@ -10,7 +10,6 @@ namespace HandBrakeWPF.ViewModels { using System; - using System.Collections.Generic; using System.Diagnostics; using System.Windows; @@ -36,6 +35,16 @@ namespace HandBrakeWPF.ViewModels /// private readonly IScanServiceWrapper scanService; + /// + /// The selected tab. + /// + private int selectedTab; + + /// + /// The encode log index. + /// + private int encodeLogIndex; + #endregion /// @@ -52,12 +61,24 @@ namespace HandBrakeWPF.ViewModels this.encodeService = encodeService; this.scanService = scanService; this.Title = "Log Viewer"; + this.encodeLogIndex = 0; } /// /// Gets or sets the selected tab. /// - public int SelectedTab { get; set; } + public int SelectedTab + { + get + { + return this.selectedTab; + } + set + { + this.selectedTab = value; + this.NotifyOfPropertyChange(() => this.SelectedTab); + } + } /// /// Gets Log. @@ -97,7 +118,7 @@ namespace HandBrakeWPF.ViewModels /// public void CopyLog() { - Clipboard.SetDataObject(this.SelectedTab == 0 ? this.ScanLog : this.EncodeLog, true); + Clipboard.SetDataObject(this.SelectedTab == 1 ? this.ScanLog : this.EncodeLog, true); } /// @@ -109,6 +130,8 @@ namespace HandBrakeWPF.ViewModels this.encodeService.EncodeCompleted += EncodeServiceEncodeCompleted; this.encodeService.EncodeStatusChanged += this.EncodeServiceEncodeStatusChanged; this.scanService.ScanStatusChanged += this.ScanServiceScanStatusChanged; + this.scanService.ScanStared += this.scanService_ScanStared; + this.encodeService.EncodeStarted += this.encodeService_EncodeStarted; base.OnActivate(); this.NotifyOfPropertyChange(() => this.ScanLog); @@ -140,7 +163,12 @@ namespace HandBrakeWPF.ViewModels /// private void EncodeServiceEncodeStatusChanged(object sender, EncodeProgressEventArgs e) { - this.NotifyOfPropertyChange(() => this.EncodeLog); + if (encodeLogIndex != this.encodeService.LogIndex || this.encodeService.LogIndex == -1) + { + this.NotifyOfPropertyChange(() => this.EncodeLog); + } + + encodeLogIndex = this.encodeService.LogIndex; } /// @@ -155,6 +183,8 @@ namespace HandBrakeWPF.ViewModels this.encodeService.EncodeCompleted -= EncodeServiceEncodeCompleted; this.encodeService.EncodeStatusChanged -= this.EncodeServiceEncodeStatusChanged; this.scanService.ScanStatusChanged -= this.ScanServiceScanStatusChanged; + this.scanService.ScanStared -= this.scanService_ScanStared; + this.encodeService.EncodeStarted -= this.encodeService_EncodeStarted; base.OnDeactivate(close); } @@ -186,5 +216,33 @@ namespace HandBrakeWPF.ViewModels { this.NotifyOfPropertyChange(() => this.EncodeLog); } + + /// + /// The encode service encode started. + /// + /// + /// The sender. + /// + /// + /// The e. + /// + private void encodeService_EncodeStarted(object sender, EventArgs e) + { + this.SelectedTab = 0; + } + + /// + /// The scan service scan stared. + /// + /// + /// The sender. + /// + /// + /// The e. + /// + private void scanService_ScanStared(object sender, EventArgs e) + { + this.SelectedTab = 1; + } } } \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs index c837627c4..8f643a74b 100644 --- a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs @@ -94,6 +94,11 @@ namespace HandBrakeWPF.ViewModels /// private readonly IEncodeServiceWrapper encodeService; + /// + /// Windows 7 API Pack wrapper + /// + private readonly Win7 windowsSeven = new Win7(); + /// /// HandBrakes Main Window Title /// @@ -163,6 +168,11 @@ namespace HandBrakeWPF.ViewModels /// The Source Menu Backing Field /// private IEnumerable sourceMenu; + + /// + /// The last percentage complete value. + /// + private int lastEncodePercentage; #endregion /// @@ -956,11 +966,15 @@ namespace HandBrakeWPF.ViewModels if (window != null) { + ILogViewModel logvm = (ILogViewModel)window.DataContext; + logvm.SelectedTab = this.IsEncoding ? 0 : 1; window.Activate(); } else { - this.WindowManager.ShowWindow(IoC.Get()); + ILogViewModel logvm = IoC.Get(); + logvm.SelectedTab = this.IsEncoding ? 0 : 1; + this.WindowManager.ShowWindow(logvm); } } @@ -1782,6 +1796,12 @@ namespace HandBrakeWPF.ViewModels /// private void EncodeStatusChanged(object sender, HandBrake.ApplicationServices.EventArgs.EncodeProgressEventArgs e) { + int percent; + int.TryParse( + Math.Round(e.PercentComplete).ToString(CultureInfo.InvariantCulture), + out percent); + + Execute.OnUIThread( () => { @@ -1796,6 +1816,13 @@ namespace HandBrakeWPF.ViewModels e.EstimatedTimeLeft, e.ElapsedTime, this.queueProcessor.Count); + + if (lastEncodePercentage != percent && this.windowsSeven.IsWindowsSeven) + { + this.windowsSeven.SetTaskBarProgress(percent); + } + + lastEncodePercentage = percent; } }); } @@ -1837,6 +1864,11 @@ namespace HandBrakeWPF.ViewModels { this.ProgramStatusLabel = "Queue Finished"; this.IsEncoding = false; + + if (this.windowsSeven.IsWindowsSeven) + { + this.windowsSeven.SetTaskBarProgressToNoProgress(); + } }); } diff --git a/win/CS/HandBrakeWPF/ViewModels/QueueViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/QueueViewModel.cs index 4f344db08..79532b9ee 100644 --- a/win/CS/HandBrakeWPF/ViewModels/QueueViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/QueueViewModel.cs @@ -101,7 +101,7 @@ namespace HandBrakeWPF.ViewModels this.errorService = errorService; this.Title = "Queue"; this.JobsPending = "No encodes pending"; - this.JobStatus = "There are no jobs currently encoding"; + this.JobStatus = "There are no jobs currently encoding"; } #endregion @@ -263,9 +263,9 @@ namespace HandBrakeWPF.ViewModels { MessageBoxResult result = this.errorService.ShowMessageBox( - "This encode is currently in progress. If you delete it, the encode will be stoped. Are you sure you wish to proceed?", - "Warning", - MessageBoxButton.YesNo, + "This encode is currently in progress. If you delete it, the encode will be stoped. Are you sure you wish to proceed?", + "Warning", + MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) @@ -330,7 +330,7 @@ namespace HandBrakeWPF.ViewModels /// public void Import() { - VistaOpenFileDialog dialog = new VistaOpenFileDialog { Filter = "HandBrake Queue Files (*.hbq)|*.hbq", CheckFileExists = true }; + VistaOpenFileDialog dialog = new VistaOpenFileDialog { Filter = "HandBrake Queue Files (*.hbq)|*.hbq", CheckFileExists = true }; dialog.ShowDialog(); this.queueProcessor.RestoreQueue(dialog.FileName); @@ -374,7 +374,7 @@ namespace HandBrakeWPF.ViewModels { this.Load(); - this.WhenDoneAction = this.userSettingService.GetUserSetting(ASUserSettingConstants.WhenCompleteAction); + this.WhenDoneAction = this.userSettingService.GetUserSetting(ASUserSettingConstants.WhenCompleteAction); this.queueProcessor.JobProcessingStarted += this.queueProcessor_JobProcessingStarted; this.queueProcessor.QueueCompleted += this.queueProcessor_QueueCompleted; @@ -414,19 +414,23 @@ namespace HandBrakeWPF.ViewModels private void EncodeService_EncodeStatusChanged( object sender, EncodeProgressEventArgs e) { - if (this.IsEncoding) + Caliburn.Micro.Execute.OnUIThread(() => { - this.JobStatus = - string.Format( - "Encoding: Pass {0} of {1}, {2:00.00}%, FPS: {3:000.0}, Avg FPS: {4:000.0}, Time Remaining: {5}, Elapsed: {6:hh\\:mm\\:ss}", - e.Task, - e.TaskCount, - e.PercentComplete, - e.CurrentFrameRate, - e.AverageFrameRate, - e.EstimatedTimeLeft, - e.ElapsedTime); - } + if (this.IsEncoding) + { + this.JobStatus = + string.Format( + "Encoding: Pass {0} of {1}, {2:00.00}%, FPS: {3:000.0}, Avg FPS: {4:000.0}, Time Remaining: {5}, Elapsed: {6:hh\\:mm\\:ss}", + e.Task, + e.TaskCount, + e.PercentComplete, + e.CurrentFrameRate, + e.AverageFrameRate, + e.EstimatedTimeLeft, + e.ElapsedTime); + } + + }); } /// diff --git a/win/CS/HandBrakeWPF/Views/LogView.xaml b/win/CS/HandBrakeWPF/Views/LogView.xaml index 94e5989ba..f8b2c9327 100644 --- a/win/CS/HandBrakeWPF/Views/LogView.xaml +++ b/win/CS/HandBrakeWPF/Views/LogView.xaml @@ -37,21 +37,21 @@ - + - - + +