I want to process items one by one, every item from a datagrid element.
I have a method which creates a background worker:
internal void Run(Action doWork, Action completed, Action loadingBeforeAction = null, Action loadingAfterAction = null)
{
using (BackgroundWorker worker = new BackgroundWorker())
{
worker.DoWork += (s, ev) =>
{
if (loadingBeforeAction != null)
{
_dispatcher.Invoke(loadingBeforeAction);
}
doWork();
};
worker.RunWorkerCompleted += (s, ev) =>
{
if (loadingAfterAction != null)
{
_dispatcher.Invoke(loadingAfterAction);
}
completed();
};
worker.RunWorkerAsync();
}
}
And now process the selected items from datagrid:
var folders = btn.Name.Equals("test")
? _model.Folders.ToArray()
: fileDataGrid.SelectedItems.Cast<FolderStatus>().ToArray();"
and
foreach (var folder in folders)
{
Run(() =>
{
Dispatcher.Invoke(()=> {_model.Message = $"This is message for {folder.Name}"});
// long operation here
}, () =>
{
// stuff
}, () =>
{
_model.IsBusy = true;
}, () =>
{
_model.IsBusy = false;
});
}
Seems that all items are processed simultaneously and process message flick from text to other depends on _model.Message
text.
How to process item one by one but without blocking the UI?