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
/// <returns>the parse status of the media</returns>
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(() =>
{
ParsedChanged -= OnParsedChanged;
......@@ -425,26 +425,25 @@ namespace LibVLCSharp.Shared
tcs?.TrySetCanceled();
});
void OnParsedChanged(object sender, MediaParsedChangedEventArgs mediaParsedChangedEventArgs)
{
tcs?.TrySetResult(mediaParsedChangedEventArgs.ParsedStatus);
}
void OnParsedChanged(object sender, MediaParsedChangedEventArgs mediaParsedChangedEventArgs)
=> tcs?.TrySetResult(mediaParsedChangedEventArgs.ParsedStatus);
try
{
cancellationToken.ThrowIfCancellationRequested();
tcs = new TaskCompletionSource<MediaParsedStatus>();
ParsedChanged += OnParsedChanged;
var result = Native.LibVLCMediaParseWithOptions(NativeReference, options, timeout);
if (result == -1)
{
tcs.TrySetResult(ParsedStatus);
tcs.TrySetResult(ParsedStatus);
}
return await tcs.Task.ConfigureAwait(false);
}
finally
{
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