diff options
| author | Bond_009 <bond.009@outlook.com> | 2019-01-29 21:39:12 +0100 |
|---|---|---|
| committer | Bond_009 <bond.009@outlook.com> | 2019-02-02 15:56:54 +0100 |
| commit | 95ee3c72e3465ec8d958b87e6c339d94db4db45a (patch) | |
| tree | ca3d135db4f94b5f6eaba6ca2c7473b5b2a876c4 /MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs | |
| parent | 66eabcdd3906cb04dfc60767dea8328267a6f134 (diff) | |
Properly dispose Tasks
Diffstat (limited to 'MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs')
| -rw-r--r-- | MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs | 84 |
1 files changed, 39 insertions, 45 deletions
diff --git a/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs index b231938b5..cf239671f 100644 --- a/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs +++ b/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs @@ -76,24 +76,26 @@ namespace MediaBrowser.MediaEncoding.Encoder var commandLineArgs = GetCommandLineArguments(encodingJob); - Process process = Process.Start(new ProcessStartInfo + Process process = new Process { - WindowStyle = ProcessWindowStyle.Hidden, - CreateNoWindow = true, - UseShellExecute = false, - - // Must consume both stdout and stderr or deadlocks may occur - //RedirectStandardOutput = true, - RedirectStandardError = true, - RedirectStandardInput = true, + StartInfo = new ProcessStartInfo + { + WindowStyle = ProcessWindowStyle.Hidden, + CreateNoWindow = true, + UseShellExecute = false, - FileName = MediaEncoder.EncoderPath, - Arguments = commandLineArgs, + // Must consume both stdout and stderr or deadlocks may occur + //RedirectStandardOutput = true, + RedirectStandardError = true, + RedirectStandardInput = true, - ErrorDialog = false - }); + FileName = MediaEncoder.EncoderPath, + Arguments = commandLineArgs, - process.EnableRaisingEvents = true; + ErrorDialog = false + }, + EnableRaisingEvents = true + }; var workingDirectory = GetWorkingDirectory(options); if (!string.IsNullOrWhiteSpace(workingDirectory)) @@ -132,50 +134,42 @@ namespace MediaBrowser.MediaEncoding.Encoder cancellationToken.Register(async () => await Cancel(process, encodingJob)); - // MUST read both stdout and stderr asynchronously or a deadlock may occur - //process.BeginOutputReadLine(); - // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback new JobLogger(Logger).StartStreamingLog(encodingJob, process.StandardError.BaseStream, encodingJob.LogFileStream); - // Wait for the file to or for the process to stop - Task file = WaitForFileAsync(encodingJob.OutputFilePath); - await Task.WhenAny(encodingJob.TaskCompletionSource.Task, file).ConfigureAwait(false); - - return encodingJob; - } + Logger.LogInformation("test0"); - public static Task WaitForFileAsync(string path) - { - if (File.Exists(path)) + if (File.Exists(encodingJob.OutputFilePath)) { - return Task.CompletedTask; + return encodingJob; } - var tcs = new TaskCompletionSource<bool>(); - FileSystemWatcher watcher = new FileSystemWatcher(Path.GetDirectoryName(path)); + Logger.LogInformation("test1"); - watcher.Created += (s, e) => + using (var watcher = new FileSystemWatcher(Path.GetDirectoryName(encodingJob.OutputFilePath))) { - if (e.Name == Path.GetFileName(path)) - { - watcher.Dispose(); - tcs.TrySetResult(true); - } - }; + var tcs = new TaskCompletionSource<bool>(); + string fileName = Path.GetFileName(encodingJob.OutputFilePath); - watcher.Renamed += (s, e) => - { - if (e.Name == Path.GetFileName(path)) + watcher.Created += (s, e) => { - watcher.Dispose(); - tcs.TrySetResult(true); - } - }; + if (e.Name == fileName) + { + tcs.TrySetResult(true); + } + }; - watcher.EnableRaisingEvents = true; + watcher.EnableRaisingEvents = true; - return tcs.Task; + Logger.LogInformation("test2"); + + // Wait for the file to or for the process to stop + await Task.WhenAny(encodingJob.TaskCompletionSource.Task, tcs.Task).ConfigureAwait(false); + + Logger.LogInformation("test3"); + + return encodingJob; + } } private async Task Cancel(Process process, EncodingJob job) |
