private void CleanupProcessInfo(object sender, EventArgs e) { if (_cpuUsageMonitor != null) { _cpuUsageMonitor.Dispose(); _cpuUsageMonitor = null; } if (_currentffmpegProcess != null) { _currentffmpegProcess.ErrorDataReceived -= OnEncodingProgress; _currentffmpegProcess.Exited -= CleanupProcessInfo; _currentffmpegProcess.CancelErrorRead(); _currentffmpegProcess.Dispose(); _currentffmpegProcess = null; } }
private bool CanSupportMoreTasks() { if (!Model.AnyTasksPending) { return(false); } if (Model.NoTasksStarted) { return(true); } var currentTotalCpuUsage = ProcessCpuMonitor.GetTotalCpuUsage(); var averageTaskCpuUsage = Model.Tasks.Where(x => x.Started).Average(x => x.CpuUsage); return(averageTaskCpuUsage < 100 - currentTotalCpuUsage); }
public void StartEncoding(CancellationToken token) { var arguments = EncodingArgs; Debug.WriteLine($"Starting ffmpeg with: \"{arguments}\""); _currentffmpegProcess = new Process { StartInfo = new ProcessStartInfo { FileName = FfmpegUtils.FfmpegFilePath, Arguments = arguments, UseShellExecute = false, RedirectStandardError = true, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden }, EnableRaisingEvents = true }; _currentffmpegProcess.ErrorDataReceived += OnEncodingProgress; _currentffmpegProcess.Exited += CleanupProcessInfo; _currentffmpegProcess.StartAsChildProcess(); _currentffmpegProcess.BeginErrorReadLine(); _ = token.Register(() => _currentffmpegProcess?.Kill()); _encodingTask.Started = true; // Give the process time to spool up _ = _currentffmpegProcess.WaitForExit(1000); // In case the process finished very quickly if (_currentffmpegProcess?.HasExited == false && !_encodingTask.CancelToken.IsCancellationRequested) { _cpuUsageMonitor = new ProcessCpuMonitor(_currentffmpegProcess); } }