Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async Method Using Generic Func<T>

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.

like image 705
Chris Mack Avatar asked May 07 '26 12:05

Chris Mack


2 Answers

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));
like image 79
Stephen Cleary Avatar answered May 09 '26 00:05

Stephen Cleary


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.

like image 42
Robert Harvey Avatar answered May 09 '26 01:05

Robert Harvey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!