I need to add in a form.listwiew some rows but i need to add them dinamyically while my async function is processing... I have read that is not possible to do this from an async method. Anyway i have to do this ... any other method or solution? Example:
private async Task start(CancellationToken token)
{
try
{
await Task.Run(() =>
{
//do something and then modify UI
Guiadjust("",1); //function that add rows
},token);
}catch (Exception ex)
{
if (ex is TaskCanceledException)
CustomMessageBox.Show("Unaxpected error", ex.ToString());
}
}
Function to add rows to list graphical form element that doesn't work because called from async method, instead it should normally work!
private void Guiadjust(string cc, int ff)
{
string[] row = { "Working", "", "" };
var listViewItem = new ListViewItem(row);
list.Items.Add(listViewItem);
}
Calling the function:
await start(cts.Token);