I have a list of information Each of these information must be in a user control, It takes a long time to create a user control And the user interface hangs so i used Task.Delay.ContinuWith ()
(In order to update the user interface after adding any item)
But there is a problem, Information is not displayed in sequence
Dispatcher.Invoke(new Action(() =>
{
waterfallFlow.Children.Clear();
var parsedValues = doc.DocumentNode.SelectNodes(...).Skip(1)
.Select(r =>
{...}).ToList();
foreach (var item in parsedValues)
{
Task.Delay(200).ContinueWith(ctx =>
{
waterfallFlow.Children.Add(_currentUser);
waterfallFlow.Refresh();
}, TaskScheduler.FromCurrentSynchronizationContext());
}
}
}), DispatcherPriority.ContextIdle, null);
Output is:
10 | Ali
9 | Hadi
1 | Hasan
15 | kajsd
...
But the information should be received as follows
1 | Hasan
2 | ad
2 | ad
3 | ad
...
Consider using the Dispatcher.Invoke<TResult>
over load and simply awaiting a Task
Dispatcher.Invoke<Task>(async () => {
waterfallFlow.Children.Clear();
var parsedValues = doc.DocumentNode.SelectNodes(...)
.Skip(1)
.Select(r => {...})
.ToList();
foreach (var item in parsedValues) {
await Task.Delay(200);
waterfallFlow.Children.Add(item);
waterfallFlow.Refresh();
}
}, DispatcherPriority.ContextIdle, null);
this should allow the items to be added in the same order they were read.