Reputation: 4575
I am trying to call my async function from a wrapper. But I am getting this compiler error. In the following code, I would like to get string x
value with most of awaits abstracted/pushed into CallAsyncFunc
code. What is the right way to do this in c# 4.5? Thanks!
error CS4010: Cannot convert async lambda expression to delegate type '
System.Func<int,string>
'. An async lambda expression may returnvoid
,Task
orTask<T>
, none of which are convertible to 'System.Func<int,string>
'.
public async Task<T2> CallAsyncFunc<T1,T2>(T1 a, Func<T1, T2> func)
{
return func.Invoke(a);
}
public async Task<string> GetString(int value)
{
await Task.Run(() => Thread.Sleep(2000));
return "" + value;
}
public async Task MainAsyncEntry()
{
string x = await CallAsyncFunc<int, string>(30, async(x) => await GetString(x));
}
Upvotes: 5
Views: 25111
Reputation: 3145
Remark 1:
await GetString(x)
unwraps the Task<string>
to string
, then CallAsyncFunc
wraps again into Task<string>
. You should rather keep the wrapped expression all the way long of your calling stack. It is the recommanded way as far as I know.
Remark 2: Resharper warns about CallAsyncFunc:
This async method lacks ‘ await’ operators and will run synchronously. Consider using the ‘await’ operator to await non-blocking API calls, or ‘ await TaskEx.Run(…)’ to do CPU-bound work on a background thread
Indeed you can ommit the async
keyword and avoid the re-wrapping into Task<string>
.
As a result of Remark 1 and Remark 2, you could write following colde:
// REALLY ASYNC METHOD, WRAPS THE RESULT INTO Task<string>
public async Task<string> GetString(int value)
{
await Task.Run(() => Thread.Sleep(2000));
return "" + value;
}
// NOT ASYNC ANY MORE: DOES NOT WRAP THE RESULT INTO Task<T2> any more
public T2 CallAsyncFunc<T1, T2>(T1 a, Func<T1, T2> func)
{
return func.Invoke(a);
}
// WAIT FOR THE ASYNC RESULT ONLY IN THE OUTER SCOPE (UNWRAPS Task<string> BY THE WAY)
string y = await CallAsyncFunc(30, GetString);
string z = await CallAsyncFunc<int, Task<string>>(30, GetString);
Upvotes: 3
Reputation: 8359
In:
string x = await CallAsyncFunc<int, string>(30, async(x) => await GetString(x));
You have two x
that the first issue. Try:
string y = await CallAsyncFunc<int, string>(30, async(x) => await GetString(x));
Here the output of the lambda is string
but because you use async
it should be a Task
or Task<T>
.
You can write:
string y = await CallAsyncFunc<int, string>(30, x => GetString(x).Result);
// Equivalent to
string y = await CallAsyncFunc(30, x => GetString(x).Result);
Or
string y = await CallAsyncFunc<int, Task<string>>(30, x => GetString(x)).Result;
// Equivalent to
string y = await CallAsyncFunc(30, GetString).Result;
But in either cases, your CallAsyncFunc
will run synchronously because it did not contain any await
.
Here the good way to do it:
// This
public async Task<TOut> CallAsyncFunc<TIn, TOut>(TIn input, Func<TIn, Task<TOut>> func)
{
return await func.Invoke(input);
}
// Or those
public Task<TOut> CallAsyncFunc<TIn, TOut>(TIn input, Func<TIn, Task<TOut>> func)
{
return func.Invoke(input);
}
public async Task<string> GetString(int value)
{
await Task.Run(() => Thread.Sleep(2000));
return value.ToString(CultureInfo.InvariantCulture);
}
public async Task MainAsyncEntry()
{
string y = await CallAsyncFunc(30, GetString);
}
Upvotes: 8