简体   繁体   中英

How to convert a Task into a Task<T>?

I'm sure this has been asked before but I can't seem to phrase the question right to get any answers that solve my question.

I've got a base class that implements an interface with a Save method that returns a Task:

public interface ICanSave
{
    Task Save();
}

public class Base : ICanSave
{
    public Task Save()
    {
        return Task.CompletedTask;
    }
}

Of course the Save method will actually do something, like saving to a database, which is why it's a Task, so it can be awaited. If I have a Foo class that inherits from the base class:

public class Foo : Base { }

Currently, you'd have to do the instantiation and save in two distinct steps:

var foo = new Foo();

await foo.Save();

However I'd like to be able to do the instantiation and the save as an expression and return a Task also. I've come up with this helper:

public static class Helper<T> where T : ICanSave
{
    public static Task<T> Save(T obj)
    {
        var source = new TaskCompletionSource<T>();

        ThreadPool.QueueUserWorkItem(_ =>
        {
            obj.Save();
            source.SetResult(obj);
        });

        return source.Task;
    }
}

Which could then be used to do:

var foo = await Helper<Foo>.Save(new Foo());

Which is more like the result I'm trying to achieve, but it seems pretty cumbersome, plus the helper seems to be pushing the Save to a Task twice (once for the original Task and a second time in the helper).

Is there a more simple way to do this conversion from a Task to a Task of T?

I don't see a point of such helper method, but if you really want you can like this:

public static class Helper<T> where T : ICanSave {
    public static async Task<T> Save(T obj) {
        await obj.Save();
        return obj;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM