Winforms - cursor doesn't stay as "WaitCursor" in long running async method

I have an async button click that awaits some long running file i/o stuff. I noticed that for REALLY long i/o stuff (like reading massive files), the wait cursor and controls don't change before hitting that await like in the first two lines of the function below. For smaller files they change without issue. I don't understand why that would matter but that's what is happening.
public List<Item> ItemsCollection { get; } = new List<Item>();
private async void ImportBtn_Click(object sender, EventArgs e)
{
try
{
// also tried Cursor.Current = Cursors.WaitCursor;
Application.UseWaitCursor = true;
SetControlsEnabledStatus(false);

foreach (var item in _items)
{
try
{
var newItem = await Importer.ImportFile(item); // async method that does async io stuff like ReadLineAsync()
ItemsCollection .Add(newItem);
}
catch (Exception ex)
{
// do some logging or something
}
}
}
catch (Exception ex)
{
// do whatever error handling
}
finally
{
Application.UseWaitCursor = false;
SetControlsEnabledStatus(true);
DialogResult = DialogResult.OK;
Close();
}
}
public List<Item> ItemsCollection { get; } = new List<Item>();
private async void ImportBtn_Click(object sender, EventArgs e)
{
try
{
// also tried Cursor.Current = Cursors.WaitCursor;
Application.UseWaitCursor = true;
SetControlsEnabledStatus(false);

foreach (var item in _items)
{
try
{
var newItem = await Importer.ImportFile(item); // async method that does async io stuff like ReadLineAsync()
ItemsCollection .Add(newItem);
}
catch (Exception ex)
{
// do some logging or something
}
}
}
catch (Exception ex)
{
// do whatever error handling
}
finally
{
Application.UseWaitCursor = false;
SetControlsEnabledStatus(true);
DialogResult = DialogResult.OK;
Close();
}
}
If I wrap Importer.ImportFile(item) in a Task.Run, then it seems to work. Not sure why or if that is the right solution.
3 Replies
douchebag
douchebag5mo ago
yo bro
oke
oke5mo ago
try using a Refresh() to force the form to refresh things right after setting the cursor. if i had to guess, its setting the cursor then going to the file without giving the form a chance to change.
Want results from more Discord servers?
Add your server