The new async language features in c# 5.0 rely heavily on the Task object, and lots of examples show that one common way to run some of your code in a Task is to start it via Task.Run(), which represents your code as a Task and runs it on a threadpool thread.
However, I also have read that one should not start long-running code on the threadpool threads, which leads me to this question: is it possible to still use all the C# async language features (such as Task, 'await', 'async') on "regular" threads and not use the threadpool? In this case, how would one get a Task object representing code running on a "regular" thread?
And as a follow up question about the rule of not running long-running code in the threadpool - is this rule only about code that is cpu intensive? What if your code runs a long time (72 hours, for example) but spends most of its time doing things like "await Task.Delay()".. is it then okay to use threadpool threads, or should one use "regular" threads in all cases where your concurrent code needs to run for a long time?
I also have read that one should not start long-running code on the threadpool threads
The thread pool will respond to long-running code correctly, so it's not like you can't do it - it's just not the most efficient way to do it.
Also note that Task.Delay implies the opposite of "long-running" - the individual Task actually completes at the point of the await and is no longer on the thread pool. Another Task is created and queued to the thread pool when Task.Delay completes. The Task returned by Task.Run (when you pass it an async delegate) is actually a "proxy Task" that represents the entire delegate.
is it possible to still use all the C# async language features (such as Task, 'await', 'async') on "regular" threads and not use the threadpool?
In your situation, you don't need it, but it is possible. My AsyncEx library includes an AsyncContextThread type that exposes a TaskFactory property you can use to start Tasks on that thread.
I'm servicing all instances "in parallel" using Task.Run()
In your case, you don't need Task.Run or a TaskFactory at all. You can easily do this in parallel without explicitly sending work off to the thread pool:
string[] instanceNames = ...;
var tasks = instanceNames.Select(ServiceInstanceAsync);
await Task.WhenAll(tasks);
private static async Task ServiceInstanceAsync(string name)
{
await DownloadAndProcessFileAsync();
await Task.Delay(15000);
await ExecuteSSHAsync();
}
You can run Tasks wherever you want by making a custom TaskScheduler.
However, if all you're doing is await Task.Delay(), you shouldn't bother.
The whole purpose of asynchronous calls is to not hold up a thread while waiting for something to happen.
While you're awaiting Task.Delay(), there aren't any threads running at all.
If you're actually running synchronous code for a long time, you can specify TaskCreationOptions.LongRunning to force it to create a new thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With