Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

c# - What is the proper way to chain Tasks when returning a Task?

I am so so with using Tasks in C# but I get confused when I try to return a Task from a method and that method will do multiple tasks within itself. So do I have my method spin up a new Task and then do everything sequentially inside of there? It's hard to wrap my head around doing it all with .ContinueWith()

Example:

public Task<string> GetSomeData(CancellationToken token)
{
    return Task.Factory.StartNew(() =>
    {
        token.ThrowIfCancellationRequested();

        var initialData = GetSomeInteger(token).Result;

        return GetSomeString(initialData, token).Result;
    });
}

public Task<int> GetSomeInteger(CancellationToken token)
{
    return Task<int>.Factory.StartNew(() =>
    {
        return 4;
    }, token);
}

public Task<string> GetSomeString(int value, CancellationToken token)
{
    return Task<string>.Factory.StartNew(() =>
    {
        return value.ToString();
    }, token);
}

I am unsure how else to write this method to make it use Tasks correctly. I guess I just feel like there should be a .ContinueWith in there or something.

Possible fix??

public Task<string> GetSomeData(CancellationToken token)
{
    return GetSomeInteger(token).ContinueWith((prevTask) =>
    {
        return GetSomeString(prevTask.Result, token);
    }, token).Unwrap();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In general, it's often best to try to avoid spinning up new tasks if you are already working with task-based methods. Chaining tasks instead of blocking explicitly will reduce the overhead of the system, as it won't keep a ThreadPool thread tied up waiting.

That being said, it's often simpler to just block as you're doing.

Note that C# 5 makes this far simpler, providing an API that gives you the best of both:

public async Task<string> GetSomeData(CancellationToken token)
{
    token.ThrowIfCancellationRequested();

    var initialData = await SomeOtherMethodWhichReturnsTask(token);

    string result = await initialData.MethodWhichAlsoReturnsTask(token);

    return result;
};

Edit after update:

Given the new code, there isn't an easy way to chain this directly with ContinueWith. There are a couple of options. You can use Unwrap to convert the Task<Task<string>> you'd create, ie:

public Task<string> GetSomeData(CancellationToken token)
{
    Task<Task<string>> task = GetSomeInteger(token)
                               .ContinueWith(t => 
                               {
                                   return GetSomeString(t.Result, token);
                               }, token);
    return task.Unwrap();
}

Alternatively, you can handle the unwrapping yourself elegantly with TaskCompletionSource<T>:

public Task<string> GetSomeData(CancellationToken token)
{
    var tcs = new TaskCompletionSource<string>();

    Task<int> task1 = GetSomeInteger(token);
    Task<Task<string>> task2 = task1.ContinueWith(t => GetSomeString(t.Result, token));
    task2.ContinueWith(t => tcs.SetResult(t.Result.Result));
    return tcs.Task;
}

This allows the entire process to work without creating a new Task (which ties up a threadpool thread), and without ever blocking.

Note that you would probably want to add continuations on cancellation, and use tcs.SetCancelled when a cancellation was requested, as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...