I would like to know how to write your own async methods the "correct" way.
I have seen many many posts explaining the async/await pattern like this:
http://msdn.microsoft.com/en-us/library/hh191443.aspx
// Three things to note in the signature:
// - The method has an async modifier.
// - The return type is Task or Task<T>. (See "Return Types" section.)
// Here, it is Task<int> because the return statement returns an integer.
// - The method name ends in "Async."
async Task<int> AccessTheWebAsync()
{
// You need to add a reference to System.Net.Http to declare client.
HttpClient client = new HttpClient();
// GetStringAsync returns a Task<string>. That means that when you await the
// task you'll get a string (urlContents).
Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
// You can do work here that doesn't rely on the string from GetStringAsync.
DoIndependentWork();
// The await operator suspends AccessTheWebAsync.
// - AccessTheWebAsync can't continue until getStringTask is complete.
// - Meanwhile, control returns to the caller of AccessTheWebAsync.
// - Control resumes here when getStringTask is complete.
// - The await operator then retrieves the string result from getStringTask.
string urlContents = await getStringTask;
// The return statement specifies an integer result.
// Any methods that are awaiting AccessTheWebAsync retrieve the length value.
return urlContents.Length;
}
private void DoIndependentWork()
{
resultsTextBox.Text += "Working........\r\n";
}
This works great for any .NET Method that already implements this functionality like
- System.IO opertions
- DataBase opertions
- Network related operations (downloading, uploading...)
But what if I want to write my own method that takes quite some time to complete where there just is no Method I can use and the heavy load is in the DoIndependentWork
method of the above example?
In this method I could do:
- String manipulations
- Calculations
- Handling my own objects
- Aggregating, comparing, filtering, grouping, handling stuff
- List operations, adding, removing, coping
Again I have stumbled across many many posts where people just do the following (again taking the above example):
async Task<int> AccessTheWebAsync()
{
HttpClient client = new HttpClient();
Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
await DoIndependentWork();
string urlContents = await getStringTask;
return urlContents.Length;
}
private Task DoIndependentWork()
{
return Task.Run(() => {
//String manipulations
//Calculations
//Handling my own objects
//Aggregating, comparing, filtering, grouping, handling stuff
//List operations, adding, removing, coping
});
}
You may notice that the changes are that DoIndependentWork
now returns a Task and in the AccessTheWebAsync
task the method got an await
.
The heavy load operations are now capsulated inside a Task.Run()
, is this all it takes?
If that's all it takes is the only thing I need to do to provide async Method for every single method in my library the following:
public class FooMagic
{
public void DoSomeMagic()
{
//Do some synchron magic...
}
public Task DoSomeMagicAsync()
{
//Do some async magic... ?!?
return Task.Run(() => { DoSomeMagic(); });
}
}
Would be nice if you could explain it to me since even a high voted question like this: How to write simple async method? only explains it with already existing methods and just using asyn/await pattern like this comment of the mentioned question brings it to the point: How to write simple async method?
First, we need to understand what an
async
method means. When one exposes anasync
method to the end user consuming theasync
method, you're telling him: "Listen, this method will return to you quickly with a promise of completing sometime in the near future". That is what you're guaranteeing to your users.Now, we need to understand how
Task
makes this "promise" possible. As you ask in your question, why simply adding aTask.Run
inside my method makes it valid to be awaited using theawait
keyword?A
Task
implements theGetAwaiter
pattern, meaning it returns an object called anawaiter
(Its actually calledTaskAwaiter
). TheTaskAwaiter
object implements eitherINotifyCompletion
orICriticalNotifyCompletion
interfaces, exposing aOnCompleted
method.All these goodies are in turn used by the compiler once the
await
keyword is used. The compiler will make sure that at design time, your object implementsGetAwaiter
, and in turn use that to compile the code into a state machine, which will enable your program to yield control back to the caller once awaited, and resume when that work is completed.Now, there are some guidelines to follow. A true async method doesn't use extra threads behind the scenes to do its job (Stephan Cleary explains this wonderfully in There Is No Thread), meaning that exposing a method which uses
Task.Run
inside is a bit misleading to the consumers of your api, because they will assume no extra threading involved in your task. What you should do is expose your API synchronously, and let the user offload it usingTask.Run
himself, controlling the flow of execution.async
methods are primarily used for I/O Bound operations, since these naturally don't need any threads to be consumed while the IO operation is executing, and that is why we see them alot in classes responsible for doing IO operations, such as hard drive calls, network calls, etc.I suggest reading the Parallel PFX teams article Should I expose asynchronous wrappers for synchronous methods? which talks exactly about what you're trying to do and why it isn't recommended.
Actual Answer
You do that using
TaskCompletionSource
, which has a Promise Task that doesn't execute any code and only:You return that task to the caller when you start the asynchronous operation and you set the result (or exception/cancellation) when you end it. Making sure the operation is really asynchronous is on you.
Here is a good example of this kind of root of all async method in Stephen Toub's
AsyncManualResetEvent
implementation:Background
There are basically two reasons to use
async-await
:I/O
intensive work (or other inherently asynchronous operations), you can call it asynchronously and so you release the calling thread and it's capable of doing other work in the mean time.CPU
intensive work, you can call it asynchronously, which moves the work off of one thread to another (mostly used forGUI
threads).So most of the .
Net
framework's asynchronous calls supportasync
out of the box and for offloading you useTask.Run
(as in your example). The only case where you actually need to implementasync
yourself is when you create a new asynchronous call (I/O
or async synchronization constructs for example).These cases are extremely rare, which is why you mostly find answers that
You can go deeper in The Nature of TaskCompletionSource
TL;DR:
Task.Run()
is what you want, but be careful about hiding it in your library.I could be wrong, but you might be looking for guidance on getting CPU-Bound code to run asynchronously [by Stephen Cleary]. I've had trouble finding this too, and I think the reason it's so difficult is that's kinda not what you're supposed to do for a library - kinda...
Article
The linked article is a good read (5-15 minutes, depending) that goes into a decent amount of detail about the hows and whys of using
Task.Run()
as part of an API vs using it to not block a UI thread - and distinguishes between two "types" of long-running process that people like to run asynchronously:The article touches on the use of API functions in various contexts, and explains whether the associated architectures "prefer" sync or async methods, and how an API with sync and async method signatures "looks" to a developer.
Answer
The last section "OK, enough about the wrong solutions? How do we fix this the right way???" goes into what I think you're asking about, ending with this:
Basically,
Task.Run()
'hogs' a thread, and is thus the thing to use for CPU-bound work, but it comes down to where it's used. When you're trying to do something that requires a lot of work and you don't want to block the UI Thread, useTask.Run()
to run the hard-work function directly (that is, in the event handler or your UI based code):But... don't hide your
Task.Run()
in an API function suffixed-Async
if it is a CPU-bound function, as basically every-Async
function is truly asynchronous, and not CPU-bound.In other words, don't call a CPU-bound function
-Async
, because users will assume it is IO-bound - just call it asynchronously usingTask.Run()
, and let other users do the same when they feel it's appropriate. Alternately, name it something else that makes sense to you (maybeBeginAsyncIndependentWork()
orStartIndependentWorkTask()
).