Async Method Using Generic Func<T>

Viewed 1893

I have written the following method:

async Task<T> Load<T>(Func<T> function)
{
    T result = await Task.Factory.StartNew(() =>
    {
        IsLoading = true;

        T functionResult = function.Invoke();

        IsLoading = false;

        return functionResult;
    });

    return result;
}

I have two questions:

  1. Can I simplify the code?

  2. I can pass any parameterless method/function to this method to get a return type of any type. E.g.:

     string GetString()
    

    By saying:

     string someString = await Load(GetString);
    

    Is there a way I could make this method more generic so that I could pass methods with parameters as well? E.g. One single method that would also accept:

     string GetString(string someString)
     string GetString(string someString, int someInt)
    

    This might look like:

     string someString = await Load(GetString("string"));
     string someString = await Load(GetString("string", 1));
    

    This obviously doesn't work, but as the Load<T> method doesn't reference the parameters, I feel this should be possible somehow.

4 Answers

Is there a way I could make this method more generic so that I could pass methods with parameters as well?

async Task<R> Load<T, R>(Func<T, R> function, T parameter)
{
    R result = await Task.Run(() =>
    {
        return function.Invoke(parameter);
    });

    return result;
}

You can bundle into T whatever parameters the function needs.

If you still want individual parameters, you will have to create additional overloads.

You can also try params to pass an arbitrary number of parameters, but they must all be of the same type unless you use runtime polymorphism in some form.

Can I simplify the code?

You can both simplify it, and make it more correct. There are a few problems with the code as-written:

  1. IsLoading is a UI-bound property, so it should be updated on the UI thread, not a background thread. Some frameworks like WPF on Windows allow you to bend the rules, but other XAML-based frameworks do not.
  2. The code currently will not ever set IsLoading to false if the loading fails.
  3. Task.Factory.StartNew should be avoided; it's a dangerous, low-level method. Use Task.Run if you need to run a method on a background thread.
async Task<T> Load<T>(Func<T> function)
{
  IsLoading = true;
  try
  {
    return await Task.Run(function);
  }
  finally
  {
    IsLoading = false;
  }
}

Is there a way I could make this method more generic so that I could pass methods with parameters as well?

You can use lambdas for this:

string someString = await Load(() => GetString("string"));
string someString = await Load(() => GetString("string", 1));
  1. You can slightly shorten it to
Task<T> Load<T>(Func<T> function)
{
    return Task.Factory.StartNew(() =>
    {
        IsLoading = true;
        var functionResult = function.Invoke();
        IsLoading = false;

        return functionResult;
    });
}

async and await are optional since you can return the task. it maybe makes sense to use try ... finally block and update IsLoading = false in the finally

  1. I agree with Robert Harvey answer regarding additional parameters

Is there a way I could make this method more generic

One option would be to nest it like so:

    public bool IsLoading { get; set; }
    
    public Task<string> GetString(string input) => Task.FromResult($"ret for input {input}");

    public async Task<TReturn> Load<TReturn>(Func<Task<TReturn>> cb)
    {
        IsLoading = true;
        var ret = await cb();
        IsLoading = false;

        return ret;
    }

    public async Task Demo()
    {
        var ret = await Load(() => GetString("input"));
    }

(I've made the GetString() method return a task too. Assuming it's easy to adjust if that makes no sense in your case.)

Related