Reputation: 6029
Consider the following code:
Task task1;
Task task2;
private void button1_Click(object sender, EventArgs e)
{
task1 = Task.Run(() =>
{
for (int i = 0; i < 100000; i++)
{
BeginInvoke(new Action<int>((int n) =>
{
listView1.Items.Add(n.ToString());
}), i);
}
});
task2 = task1.ContinueWith(t =>
{
MessageBox.Show("Why isn't this reached");
}, TaskScheduler.FromCurrentSynchronizationContext());
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(String.Format("task1 status:{0}\r\ntask2 status:{1}", task1.Status, task2.Status));
}
If I run this on Windows 8, .NET Framework 4.5.2, I get the follwing output when I click button2:
task1 status:RanToCompletion
task2 status:WaitingToRun
How is this possible even though I used ContinueWith?
I know that BeginInvoke just enqueues the delegate for execution, if I use Invoke it works just fine. Is this a bug or is it interfering with BeginInvoke? Any ideas? Thanks in advance.
Upvotes: 2
Views: 343
Reputation: 127543
I don't have a primary source, but this post states that the message queue has a limit of 10,000 records(Hans Passant is one of the most experts on this site for windows forms so I trust his word that the limit exists).
Because you are putting 100,000 records on there it is either pushing your ContinueWith off the stack or never getting added there in the first place (I am not sure which)
Upvotes: 1