Bluegrams / YoutubeDLSharp

A simple .NET wrapper library for youtube-dl and yt-dlp
BSD 3-Clause "New" or "Revised" License
173 stars 30 forks source link

RunVideoDataFetch temporarily blocks main thread #7

Closed adanvdo closed 2 years ago

adanvdo commented 2 years ago

https://github.com/Bluegrams/YoutubeDLSharp/blob/74b79e28a1e3bd1b058991e48d531c64ca36caa7/YoutubeDLSharp/YoutubeDL.cs#L119

I am using your library in a WinForms app. Although I am calling await ytdl.RunVideoDataFetch(url) the UI thread is temporarily blocked and the program freezes for a couple seconds.

It seems like one or more lines between line 122 and 131 are taking longer than expected and blocking the UI. I did not dig into it much, but this was the only thing I noticed at a glance that could possibly be blocking the thread.

var opts = GetDownloadOptions();

Moving GetDownloadOptions to an async Task and awaiting that task for the options may fix the issue.

adanvdo commented 2 years ago

I tried making a couple things async in the RunVideoDataFetch method, but I still experienced a block in the UI. I havent been able to figure out what's causing it. I don't know if it has to do with the progress reporting events or what. Just FYI

adanvdo commented 2 years ago

I have found the problem. YoutubeDLProcess.cs executes process.Start() synchronously on the main thread.

you need to change if(!process.Start())

to if(!await Task.Run(() => process.Start()))

With this change the main thread is no longer blocked. Let me know if you want me to open a PR. It's a pretty simple change if you want to do it yourself. @TheMulti0 @alxnull

alxnull commented 2 years ago

Hey @adanvdo, thanks for investigating this. Would be happy to accept a PR with the fix you described!