Good, bad headline, but I couldn’t come up with a better name. My question is probably not even specific to async / await, but my question arises while processing asynchronous messages, so I'm going to present it like this:
I have several methods that create task lists and then do "wait for Task.WhenAll (task list)". The specific form of expected tasks in these methods varies. For example, some methods expect a list from Task<String> , while others expect a list from Task<foo> .
What I find is that I need to do some non-trivial try / catch processing around Task.WhenAll () in each of these methods, and this code is always the same. I would like to move this code to a generic method and then go to the task list and get this generic problem and then WhenAll wrapped in try / finally.
But the problem that I encountered is that each of the methods that call this method will pass lists of different types of tasks, and this causes a compiler complaint when I declare a parameter with my common method as simple Task:
methodA: List<Task<String>> myTaskList = ... ExecuteTasks(myTaskList); methodB: List<Task<Foo>> myTaskList = ... ExecuteTasks(myTaskList); async Task ExecuteTasks(List<Task> taskList) { try { await Task.WhenAll(taskList) } catch { ..common catch handling goes here. This handling isn't really sensitive to the ..type of the Tasks, we just need to examine it Status and Exception properties.. } }
In the above, method A and method B have their own task lists that need to be passed to ExecuteTasks, but the question is how to define a task list for ExecuteTasks so that the compiler does not complain about the type of mismatch? In a non-core world, I would probably define the ExecuteTasks parameter of the superclass of the list types of methods A and methodB so that the compiler can “speed up” them, but this approach does not seem to work here. (I tried to define ExecuteTasks as accepting a Task<Object> , but this did not solve the type mismatch problem)
source share