0

I want to run listOrders() on a separate thread and make it awaitable. I have a method of this form, that compiles and works OK

private Task<ClearedOrdersReport> listOrdersAwaitable(int a, int b)
{
    Task<ClearedOrdersReport> t = Task.Run(() =>
       listOrders(a, b));
    return t;
}

I now want to add a second line to execute before the listOrders() call. I thought it would be trivial to change the single line lambda into a block style lambda, but I cannot figure out what the correct syntax is to get it to compile ? I am probably missing something obvious, but any help would be appreciated. Thanks

11
  • Why are you wrapping listOrders in a Task.Run call? Commented Jul 8, 2022 at 0:37
  • 1
    Your question of "wanting to run listOrders() on a separate thread" doesn't make a lot of sense to me. If you just make it async, and await it, it automatically decides where to run it with the Task Scheduler. Commented Jul 8, 2022 at 0:37
  • Also, by you just calling the function (and it not being async) -- it's already running as a task Commented Jul 8, 2022 at 0:38
  • 1
    I think more information about the bigger picture needs to be stated, because what you are asking is not making a lot of sense. Commented Jul 8, 2022 at 0:40
  • @IlanKeshet - No, it doesn't. There Is No Thread. Commented Jul 8, 2022 at 1:13

2 Answers 2

3

You should use a statement lambda and return the result of listOrders.

 private Task<ClearedOrdersReport> listOrdersAwaitable(int a, int b)
 {
        Task<ClearedOrdersReport> t = Task.Run(() => {
           //add your second line here. 
           return listOrders(a, b);
        });
        return t;
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for all of the responses. This solved it for me, my silly oversight was that in going from a single line lambda to a block lambda you dont have the implicit return statement. I forgot to add the return statement before listOrders().
Don't forget to mark the answer as accepted, good luck.
0

There are some questions about what you are trying to do with the async/await elements, but one to call two many things where there was one is to use a helper method:

listOrders(a, b));

can become

DoWork(a,b);

and then

ClearedOrdersReport DoWork(int a, int b)
{
 ...
 return listOrders(a, b); 
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.