Suppose a UI update takes quite a long time. As an example, this dummy label takes 1 second to change the text.
public class MyLabel : Label
{
public override string Text
{
get { return base.Text; }
set
{
Debug.WriteLine("Setting " + value);
Thread.Sleep(1000);
base.Text = value;
}
}
}
If there are multiple tasks who want to update it, it seems those posts are queued.
var UIContext = SynchronizationContext.Current;
for (int i = 0; i < 10; i++)
{
Task.Factory.StartNew((id) =>
{
Debug.WriteLine(id);
Thread.Sleep((int)id*100);
UIContext.Post((label) =>
{
myLabel1.Text = label.ToString();
}, id);
}, i);
}
It seems the label receives the posts are queued, and the label receives them all eventually, but in this example, the next one overwrites the previous one so the previous one needs not be handled. Ideally, I would like to disregard all posts other than the last one. Can I see the queued items and remove them?