c# - Why this task hangs? -


[testmethod] public void y() {     int = 0;     new task(() => i++).wait();      assert.areequal(1, i); } 

for reason task in above example waiting forever? how should wait() task not hang?

you did not start task. try this:

[testmethod] public void y() {     int = 0;     task task = new task(() => i++);     task.start();     task.wait();     assert.areequal(1, i); } 

or better (as damien suggested) use task.run():

[testmethod] public void y() {     int = 0;     task.run(() => i++).wait();     assert.areequal(1, i); } 

Comments