// Executes a callback operation and stops the encode when a communication exception occurs. private void StopOnException(Action action) { try { action(); } catch (CommunicationException exception) { WorkerLogger.Log("Got exception: " + exception, isError: true); this.StopEncodeIfPossible(); } }
static void Main(string[] args) { try { if (args.Length < 2) { PrintUsage(); return; } int parentProcId; if (!int.TryParse(args[0], out parentProcId)) { PrintUsage(); return; } PipeName = args[1]; parentCheckTimer = new System.Timers.Timer(); parentCheckTimer.Interval = ParentCheckInterval; parentCheckTimer.AutoReset = true; parentCheckTimer.Elapsed += (o, e) => { try { if (!ProcessExists(parentProcId)) { // If we couldn't stop the process, just wait until next tick. May have not started yet or may // already be in the process of closing. if (HandBrakeEncoder.CurrentEncoder != null && HandBrakeEncoder.CurrentEncoder.StopEncodeIfPossible()) { // If we are able to stop the encode, we will do so. Cleanup should // happen with the encode complete callback. Console.WriteLine("Parent no longer exists, stopping encode."); } } } catch (Exception exception) { WorkerLogger.Log("Exception in parentCheckTimer.Elapsed: " + exception.ToString(), isError: true); throw; } }; parentCheckTimer.Start(); ServiceHost host = null; try { host = new ServiceHost(typeof(HandBrakeEncoder)); host.AddServiceEndpoint( typeof(IHandBrakeEncoder), new NetNamedPipeBinding(), "net.pipe://localhost/" + PipeName); host.Open(); encodeComplete = new ManualResetEventSlim(false); Console.WriteLine("Service state is " + host.State + " on pipe " + PipeName); encodeComplete.Wait(); host.Close(); } catch (CommunicationException exception) { WorkerLogger.Log("Exception when trying to establish pipe service: " + exception, isError: true); if (host != null) { host.Abort(); } } catch (TimeoutException exception) { WorkerLogger.Log("Exception when trying to establish pipe service: " + exception, isError: true); if (host != null) { host.Abort(); } } catch (Exception) { if (host != null) { host.Abort(); } throw; } } catch (Exception exception) { WorkerLogger.Log("Exception in Main: " + exception, isError: true); throw; } }
public void StartEncode(EncodeJob job, bool preview, int previewNumber, int previewSeconds, double overallSelectedLengthSeconds, int verbosity, int previewCount, bool useDvdNav) { CurrentEncoder = this; this.callback = OperationContext.Current.GetCallbackChannel <IHandBrakeEncoderCallback>(); try { if (this.callback == null) { throw new ArgumentException("Could not get callback channel."); } HandBrakeUtils.MessageLogged += (o, e) => { this.StopOnException(() => { this.callback.OnMessageLogged(e.Message); }); }; HandBrakeUtils.ErrorLogged += (o, e) => { this.StopOnException(() => { this.callback.OnErrorLogged(e.Message); }); }; HandBrakeUtils.SetDvdNav(useDvdNav); this.instance = new HandBrakeInstance(); this.instance.Initialize(verbosity); this.instance.ScanCompleted += (o, e) => { try { Title encodeTitle = this.instance.Titles.FirstOrDefault(title => title.TitleNumber == job.Title); if (encodeTitle != null) { lock (this.encodeLock) { this.instance.StartEncode(job, preview, previewNumber, previewSeconds, overallSelectedLengthSeconds, previewCount); this.callback.OnEncodeStarted(); this.state = EncodeState.Encoding; } } else { this.callback.OnEncodeComplete(true); this.CleanUpAndSignalCompletion(); } } catch (Exception exception) { this.callback.OnException(exception.ToString()); this.CleanUpAndSignalCompletion(); } }; this.instance.EncodeProgress += (o, e) => { this.StopOnException(() => { this.callback.OnEncodeProgress(e.AverageFrameRate, e.CurrentFrameRate, e.EstimatedTimeLeft, e.FractionComplete, e.Pass); }); }; this.instance.EncodeCompleted += (o, e) => { this.state = EncodeState.Finished; try { this.callback.OnEncodeComplete(e.Error); } catch (CommunicationException exception) { WorkerLogger.Log("Got exception when reporting completion: " + exception, isError: true); } finally { this.CleanUpAndSignalCompletion(); } }; this.instance.StartScan(job.SourcePath, previewCount, job.Title); this.state = EncodeState.Scanning; } catch (Exception exception) { this.callback.OnException(exception.ToString()); throw; } }