C # How to use a generic method?

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)

+4
source share
3 answers

Try entering ExecuteTasks instead of IEnumerable<Task> :

async Task ExecuteTasks(IEnumerable<Task> taskList) {

As Hamish Smith noted, this is a problem of covariance.

 List<Task<String>> myTaskList = ... ExecuteTasks(myTaskList); async Task ExecuteTasks(IEnumerable<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.. } } 

If it were typed on a List<Task> , then you could do something stupid:

 List<Task<String>> myTaskList = ... ExecuteTasks(myTaskList); async Task ExecuteTasks(List<Task> taskList) { taskList.Add(new Task<int>()) // bad stuff } 
+2
source
 var intTask1 = Task.Run(() => 1); var intTask2 = Task.Run(() => 2); var intTasks = new List<Task<int>> { intTask1, intTask2 }; var intExecutor = new TaskExecutor<int>(); await intExecutor.ExecuteTasks(intTasks); var stringTask1 = Task.Run(() => "foo"); var stringTask2 = Task.Run(() => "bar"); var stringTasks = new List<Task<string>> { stringTask1, stringTask2 }; var stringExecutor = new TaskExecutor<string>(); await stringExecutor.ExecuteTasks(stringTasks); 

..................................

 class TaskExecutor<T> { public async Task ExecuteTasks(IEnumerable<Task<T>> tasks) { try { await Task.WhenAll(tasks); } catch (Exception ex) { // Handle exception } } } 
+1
source

While I really should point you to Eric Lippert's series on the contradiction and covariance and how the generic tools are considered by the compiler (http://blogs.msdn.com/b/ericlippert/archive/2007/10/16/covariance- and-contravariance-in-c-part-one.aspx) ...
I really wonder if the general method will work here?

  async Task ExecuteTasks<T>(List<Task<T>> 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.. } } 
0
source

Source: https://habr.com/ru/post/1445865/


All Articles