private static void HandleException(Exception exception) { DispatchService.BeginInvoke(() => { MessageBox.Show(exception.Message); }); }
private void OnUpdateStateChanged(object sender, EventArgs e) { DispatchService.BeginInvoke(() => { this.RefreshUpdateStatus(); }); }
public void OnEncodeProgress(float averageFrameRate, float currentFrameRate, TimeSpan estimatedTimeLeft, float fractionComplete, int pass) { // Dispatch to avoid deadlocks on callbacks DispatchService.BeginInvoke(() => { lock (this.encoderLock) { this.lastWorkerCommunication = DateTimeOffset.UtcNow; if (this.encoding && this.EncodeProgress != null) { this.EncodeProgress( this, new EncodeProgressEventArgs { AverageFrameRate = averageFrameRate, CurrentFrameRate = currentFrameRate, EstimatedTimeLeft = estimatedTimeLeft, FractionComplete = fractionComplete, Pass = pass }); } } }); }
public static void AllowSleep() { DispatchService.BeginInvoke(() => { SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS); }); }
public static void PreventSleep() { DispatchService.BeginInvoke(() => { SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_SYSTEM_REQUIRED); }); }
private void ShowStatusMessage(StatusMessage message) { DispatchService.BeginInvoke(() => { this.statusTextBlock.Text = message.Message; this.statusText.Visibility = Visibility.Visible; var storyboard = (Storyboard)this.FindResource("statusTextStoryboard"); storyboard.Stop(); storyboard.Begin(); }); }
/// <summary> /// Remove from unused resources /// </summary> /// <param name="stringsToDelete"></param> public void RemoveFromUnusedResources(HashSet <string> stringsToDelete) { DispatchService.BeginInvoke(() => { for (var i = this.UnusedResources.Count - 1; i >= 0; i--) { if (stringsToDelete.Contains(this.UnusedResources[i].Key)) { this.UnusedResources.RemoveAt(i); } } }); }
public void ScanNextJob() { EncodeJobViewModel jobVM = itemsToScan[currentJobIndex]; var onDemandInstance = new HandBrakeInstance(); onDemandInstance.Initialize(Config.LogVerbosity); onDemandInstance.ScanProgress += (o, e) => { lock (this.currentJobIndexLock) { this.currentJobProgress = e.Progress; this.RaisePropertyChanged(() => this.Progress); } }; onDemandInstance.ScanCompleted += (o, e) => { jobVM.HandBrakeInstance = onDemandInstance; if (onDemandInstance.Titles.Count > 0) { Title titleToEncode = Utilities.GetFeatureTitle(onDemandInstance.Titles, onDemandInstance.FeatureTitle); jobVM.Job.Title = titleToEncode.TitleNumber; jobVM.Job.Length = titleToEncode.Duration; jobVM.Job.ChapterStart = 1; jobVM.Job.ChapterEnd = titleToEncode.Chapters.Count; } lock (this.currentJobIndexLock) { this.currentJobIndex++; this.currentJobProgress = 0; this.RaisePropertyChanged(() => this.Progress); if (this.ScanFinished) { DispatchService.BeginInvoke(() => { this.CancelCommand.Execute(null); }); } else { this.ScanNextJob(); } } }; onDemandInstance.StartScan(jobVM.Job.SourcePath, Config.PreviewCount, 0); }
private static void UpdateCornerImage(Image image, Grid imageHolder, BitmapSource bitmap, RegionChooser regionChooser, bool isRetry = false) { // Image dimensions int imageWidth = bitmap.PixelWidth; int imageHeight = bitmap.PixelHeight; double availableWidth = imageHolder.ActualWidth; double availableHeight = imageHolder.ActualHeight; if (availableWidth == 0 || availableHeight == 0) { // Intermittent bug causing this. In this case we queue it up to go again after a layout pass has been done. if (!isRetry) { DispatchService.BeginInvoke(() => { UpdateCornerImage(image, imageHolder, bitmap, regionChooser, isRetry: true); }); } return; } int cornerWidthPixels = (int)(availableWidth / ZoomedPixelSize); int cornerHeightPixels = (int)(availableHeight / ZoomedPixelSize); // Make sure the subsection of the image is not larger than the image itself cornerWidthPixels = Math.Min(cornerWidthPixels, imageWidth); cornerHeightPixels = Math.Min(cornerHeightPixels, imageHeight); double cornerWidth = cornerWidthPixels * ZoomedPixelSize; double cornerHeight = cornerHeightPixels * ZoomedPixelSize; var croppedBitmap = new CroppedBitmap(); croppedBitmap.BeginInit(); croppedBitmap.SourceRect = regionChooser(imageWidth, imageHeight, cornerWidthPixels, cornerHeightPixels); croppedBitmap.Source = bitmap; croppedBitmap.EndInit(); image.Source = croppedBitmap; image.Width = cornerWidth; image.Height = cornerHeight; }
private void ProcessPreviewImageWork(object state) { PreviewImageJob imageJob; bool workLeft = true; while (workLeft) { lock (this.imageSync) { if (this.previewImageWorkQueue.Count == 0) { this.previewImageQueueProcessing = false; return; } imageJob = this.previewImageWorkQueue.Dequeue(); } string imagePath = Path.Combine( Utilities.ImageCacheFolder, Process.GetCurrentProcess().Id.ToString(CultureInfo.InvariantCulture), imageJob.UpdateVersion.ToString(CultureInfo.InvariantCulture), imageJob.PreviewNumber + ".bmp"); BitmapSource imageSource = null; // Check the disc cache for the image lock (imageJob.ImageFileSync) { if (File.Exists(imagePath)) { // When we read from disc cache the image is already transformed. var bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.CacheOption = BitmapCacheOption.OnLoad; bitmapImage.UriSource = new Uri(imagePath); bitmapImage.EndInit(); bitmapImage.Freeze(); imageSource = bitmapImage; } } if (imageSource == null) { // Make a HandBrake call to get the image imageSource = imageJob.ScanInstance.GetPreview(imageJob.EncodeJob.HbJob, imageJob.PreviewNumber); // Transform the image as per rotation and reflection settings VCProfile profile = imageJob.EncodeJob.EncodingProfile; if (profile.FlipHorizontal || profile.FlipVertical || profile.Rotation != VCPictureRotation.None) { imageSource = CreateTransformedBitmap(imageSource, profile); } // Start saving the image file in the background and continue to process the queue. ThreadPool.QueueUserWorkItem( this.BackgroundFileSave, new SaveImageJob { PreviewNumber = imageJob.PreviewNumber, UpdateVersion = imageJob.UpdateVersion, FilePath = imagePath, Image = imageSource, ImageFileSync = imageJob.ImageFileSync }); } lock (this.imageSync) { if (imageJob.UpdateVersion == updateVersion) { this.previewImageCache[imageJob.PreviewNumber] = imageSource; if (this.SelectedPreview == imageJob.PreviewNumber) { DispatchService.BeginInvoke(() => { this.previewBitmapSource = imageSource; this.RefreshFromBitmapImage(); }); } } if (this.previewImageWorkQueue.Count == 0) { workLeft = false; this.previewImageQueueProcessing = false; } } } }
private void previewImageRefreshTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { this.waitingOnRefresh = false; this.lastImageRefreshTime = DateTime.MinValue; DispatchService.BeginInvoke(this.RefreshPreviews); }
public void StartEncode(VCJob job, ILogger logger, bool preview, int previewNumber, int previewSeconds, double overallSelectedLengthSeconds) { this.logger = logger; this.logger.Log("Starting encode in-process"); this.encoding = true; this.encodeStartEvent = new ManualResetEventSlim(false); this.encodeEndEvent = new ManualResetEventSlim(false); this.instance = new HandBrakeInstance(); this.instance.Initialize(Config.LogVerbosity); this.instance.ScanCompleted += (o, e) => { try { Title encodeTitle = this.instance.Titles.FirstOrDefault(title => title.TitleNumber == job.Title); if (encodeTitle != null) { lock (this.encoderLock) { this.instance.StartEncode(job.HbJob, preview, previewNumber, previewSeconds, overallSelectedLengthSeconds, Config.PreviewCount); this.IsEncodeStarted = true; if (this.EncodeStarted != null) { this.EncodeStarted(this, new EventArgs()); } this.encodeStartEvent.Set(); } } else { if (this.EncodeCompleted != null) { this.EncodeCompleted(this, new EncodeCompletedEventArgs { Error = true }); } this.encodeStartEvent.Set(); this.encodeEndEvent.Set(); } } catch (Exception exception) { this.logger.LogError("Encoding failed. Please report this error so it can be fixed in the future:" + Environment.NewLine + exception); } }; this.instance.EncodeProgress += (o, e) => { // Dispatch to avoid deadlocks on callbacks DispatchService.BeginInvoke(() => { lock (this.encoderLock) { if (this.encoding && this.EncodeProgress != null) { this.EncodeProgress(this, e); } } }); }; this.instance.EncodeCompleted += (o, e) => { if (this.encoding) { if (this.EncodeCompleted != null) { this.EncodeCompleted(this, e); } this.encoding = false; } this.encodeEndEvent.Set(); this.instance.Dispose(); }; this.instance.StartScan(job.SourcePath, Config.PreviewCount, job.Title); this.encoding = true; }