Commit 3e66d2ac authored by Martin Finkel's avatar Martin Finkel

Move TaskCompletionSource allocation after throw. Cannot do so for...

Move TaskCompletionSource allocation after throw. Cannot do so for cancellationTokenRegistration because of the try scope
parent e4eecbab
...@@ -417,7 +417,7 @@ namespace LibVLCSharp.Shared ...@@ -417,7 +417,7 @@ namespace LibVLCSharp.Shared
/// <returns>the parse status of the media</returns> /// <returns>the parse status of the media</returns>
public async Task<MediaParsedStatus> Parse(MediaParseOptions options = MediaParseOptions.ParseLocal, int timeout = -1, CancellationToken cancellationToken = default) public async Task<MediaParsedStatus> Parse(MediaParseOptions options = MediaParseOptions.ParseLocal, int timeout = -1, CancellationToken cancellationToken = default)
{ {
var tcs = new TaskCompletionSource<MediaParsedStatus>(); TaskCompletionSource<MediaParsedStatus> tcs = null;
var cancellationTokenRegistration = cancellationToken.Register(() => var cancellationTokenRegistration = cancellationToken.Register(() =>
{ {
ParsedChanged -= OnParsedChanged; ParsedChanged -= OnParsedChanged;
...@@ -425,26 +425,25 @@ namespace LibVLCSharp.Shared ...@@ -425,26 +425,25 @@ namespace LibVLCSharp.Shared
tcs?.TrySetCanceled(); tcs?.TrySetCanceled();
}); });
void OnParsedChanged(object sender, MediaParsedChangedEventArgs mediaParsedChangedEventArgs) void OnParsedChanged(object sender, MediaParsedChangedEventArgs mediaParsedChangedEventArgs)
{ => tcs?.TrySetResult(mediaParsedChangedEventArgs.ParsedStatus);
tcs?.TrySetResult(mediaParsedChangedEventArgs.ParsedStatus);
}
try try
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
tcs = new TaskCompletionSource<MediaParsedStatus>();
ParsedChanged += OnParsedChanged; ParsedChanged += OnParsedChanged;
var result = Native.LibVLCMediaParseWithOptions(NativeReference, options, timeout); var result = Native.LibVLCMediaParseWithOptions(NativeReference, options, timeout);
if (result == -1) if (result == -1)
{ {
tcs.TrySetResult(ParsedStatus); tcs.TrySetResult(ParsedStatus);
} }
return await tcs.Task.ConfigureAwait(false); return await tcs.Task.ConfigureAwait(false);
} }
finally finally
{ {
cancellationTokenRegistration.Dispose(); cancellationTokenRegistration.Dispose();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment