The purpose of the following code is to include any given function in the expected function. The idea is to use it when retrieving data from db, giving the code the flexibility to either use synchronous fetch functions (overlapping my current ORM) or use the same functions as async.
I know that there can be a lot of things wrong with the code concept. At the moment, I was just trying to get rid of compiler errors, so I can run the code and check its behavior. But, of course, I am open to discuss the concept in advance, and if the whole idea behind is not right, use my time more efficiently to find another solution.
async static void Main() { // The following line gives a compiler error: // Error 1 The best overloaded method match for 'CastFuncToTask<int>(System.Func<int>)' has some invalid arguments int task = await CastFuncToTask<int>(TestFunc(2)); } private static Task<T> CastFuncToTask<T>(Func<T> func) { TaskCompletionSource<T> taskCompletionSource = new TaskCompletionSource<T>(); T result = func.Invoke(); taskCompletionSource.SetResult(result); return taskCompletionSource.Task; } private static int TestFunc(int testInt) { return testInt * 2; }
c # task-parallel-library async-await
Xavier peña
source share