TPL tasks execute sub method calls in wrong order. RRS feed

  • Question

  • Hi everyone,

    I am currently trying to get my head around TPL as I have some stuff I would like to execute with this great feature. But the tasks do their work not in the correct order. This is my pesudo code which I want to run.

    void myMethod(){
    	List<Action> myActions = new List<Action>();
    	if (cond1) 
    	   myActions.Add(new Action(meth1));
    	if (cond2)
    	   myActions.Add(new Action(meth2));
    void meth1(){
    void meth2(){

    When the code gets executed I always get an exception thrown from meth2's call of stuff2() because it depends on something which is done in stuff1(). How can I keep the order of what the tasks themselves do? Obviously I always want to have stuff1() called before stuff2() within meth2(). What did I miss?

    Best regards.

    Tuesday, October 27, 2015 9:21 PM

All replies

  • Hi

    With pseudo code as it is, the only thing can happen is that stuff1() returns task, that you don't wait and call stuff2() before stuff1() is completed.

    If it doesn't help, then pseudo code needs to be closer to real code for other suggestions :)

    Wednesday, October 28, 2015 8:12 AM
  • You are going to have to wait for Stuff1 to complete before you start stuff2

    await stuff1();

    await stuff2();

    Saturday, October 31, 2015 2:48 PM