0

What would be an appropriate way to re-write my SlowMethodAsync async method, which executes a long running task, that can be awaited, but without using Task.Run?

I can do it with Task.Run as following:

public async Task SlowMethodAsync()
{
    await Task.Run(() => SlowMethod());
}

public void SlowMethod()
{
    //heavy math calculation process takes place here
}

The code, as it shown above, will spawn a new thread from a thread-pool. If it can be done differently, will it run on the invocation thread, and block it any way, as the SlowMethod content is a solid chunk of math processing without any sort of yielding and time-slice surrendering.

Just to clarify that I need my method to stay asynchronous, as it unblocks my UI thread. Just looking for another possible way to do that in a different way to how it's currently done, while keeping async method signature.

2
  • 4
    If it's all synchronous, why do you want it to be async? Commented Jan 15, 2015 at 0:26
  • If it's library code, don't do it. If it's application code, depends on the type of application. Commented Jan 15, 2015 at 7:32

2 Answers 2

4

async methods are meant for asynchronous operations. They enable not blocking threads for non-CPU-bound work. If your SlowMethod has any IO-bound operations which are currently executed synchronously (e.g. Stream.Read or Socket.Send) you can exchange these for async ones and await them in your async method.

In your case (math processing) the code's probably mostly CPU-bound, so other than offloading the work to a ThreadPool thread (using Task.Run) there's no reason to use async as all. Keep SlowMethod as it is and it will run on the calling thread.


Regarding your update: You definitely want to use Task.Run (or one of the Task.Factory.StartNew overloads) to offload your work to a different thread.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. "Non-CPU-bound work" is what I was looking for. All framework *Async methods are IO bound, so I shouldn't be trying to replicate them in the first place.
2

Actually, for your specific case, you should use

await Task.Factory.StartNew(() => SlowMethod(), TaskCreationOptions.LongRunning)

which will allow the scheduler to run your synchronous task in the appropriate place (probably in a new thread) so it doesn't gum up the ThreadPool (which isn't really designed for CPU heavy workloads).

Wouldn't it be better just to call SlowMethod synchronously? What are you gaining by awaiting it?

2 Comments

Running SlowMethod method asynchronously allows UI thread to proceed.
So this is probably a decent solution for offloading the work onto another thread while allowing the UI to proceed. If you do too much of this in the ThreadPool, you might start experiencing unexpected IO and timer latency because you share the ThreadPool with a helluva lot of framework code and should treat it with respect! ThreadPool starvation can become a real problem when the ThreadPool threads get tied up doing long-running tasks.

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.