I expect the variable in the loop to output 1,2,3 to my listbox below. But it outputs 2 2 2
what is wrong?
C# Code
public partial class Tester : Form
{
public int test = 1;
............................
private void button1_Click(object sender, EventArgs e)
{
test++;
for (int i = 0; i < 3; i++)
{
Task t = Task.Factory.StartNew(() =>
{
System.Threading.Thread.Sleep(5000);
}).ContinueWith(o =>
{
listBox1.Items.Add(test);
}, TaskScheduler.FromCurrentSynchronizationContext());
}
}