A
A
Anton_repr2020-05-26 17:49:12
C++ / C#
Anton_repr, 2020-05-26 17:49:12

How do you know that an async method has done its job?

I have 2 methods. The first downloads audio and video, and the second combines them.
I have an error - video and audio start to merge before the video is completely downloaded. What should I do?

public async void DownloadVideo1080P(string path, ProgressBar progressBar, string link)
{
    await ytClient.Videos.Streams.DownloadAsync(audioInfo, Path.Combine(newLink, $"audio.wav"));
    await ytClient.Videos.Streams.DownloadAsync(videoInfo, Path.Combine(newLink, $"video.mp4"), progressHandler);
}

public async void MergeAudioVideo(string link)
{
    await ffmpeg.ExecuteAsync([email protected]"-i video.mp4  -i audio.wav -c:v copy -c:a aac newVideo.mp4");
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
ayazer, 2020-05-26
@Anton_repr

async void
will return control back without waiting for the execution of asynchronous code inside. you need to return the task
public async Task DownloadVideo1080PAsync(string path, ProgressBar progressBar, string link)
{
    await ytClient.Videos.Streams.DownloadAsync(audioInfo, Path.Combine(newLink, $"audio.wav"));
    await ytClient.Videos.Streams.DownloadAsync(videoInfo, Path.Combine(newLink, $"video.mp4"), progressHandler);
}

public Task MergeAudioVideoAsync(string link)
{
    return ffmpeg.ExecuteAsync([email protected]"-i video.mp4  -i audio.wav -c:v copy -c:a aac newVideo.mp4");
}

....
  await DownloadVideo1080PAsync(....);
  await MergeAudioVideoAsync(...);
....

F
freeExec, 2020-05-26
@freeExec

Remove await, return tasks and wait for them to complete - https://docs.microsoft.com/en-us/dotnet/api/system...
You need to understand that an asynchronous method returns control as soon as it encounters await. Therefore, it is easy to call two of your methods in a row and hope for the best.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question