I have a long operation that involves calling some async
functions.
These work fine, but I'd like to keep my UI alive so the user can move the window and click a cancel button. To that end, I'm using BackgroundWorker.
However, I'm having issues where my code is still running after BackgroundWorker thinks the operation has completed. As best I can tell, it's because I'm using await within DoWork()
.
If so, what are the options here? How can I call these methods, which require await
, and still leave my UI active? Is there an option in WinForms?
Update
Here's some code:
private async void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
string path = Program.GetDocumentsSubfolder("Uploads");
string[] files = Directory.GetFiles(path, "*.upload");
if (files.Length > 0)
{
YouTubeApi api = new();
api.StatusChanged += Api_StatusChanged;
foreach (string file in files)
{
await api.UploadVideo(file);
}
}
}