Hi all,
may be the question will seem too advanced for a newbie like me, but I really want to write this program. I am asking for a good advice on how to implement the desired logic.
I have multiple classes, which implement the same interface
class MyClass1: IMyInterface //The same for MyClass2, MyClass3 etc. { //Many many properties and methods async Task<MyResultObject> IMyInterface.FunctionAsync(); }
Now I create one instance of each class Instance1, Instance2, etc.
Instancei=(IMyInterface)new MyClassi();
What I want to do is to asynchronously call the functions FunctionAsync() and process the results as they appear.
{
//Somewhere in constructor
List<IMyInterface> classList=new List<IMyInterface>(); classList.Add(Instance1); classList.Add(Instance2); //etc Task<MyResultObject>[] taskList = new Task<MyResultObject>[classList.Count];
} foreach(var ins in classList) { taskList[i]=ins.FunctionAsync(); taskList[i].ContinueWith((num, opt)=>{MyProcessFunction(num,opt);}i,null) }
The questions are:
- Will this work, or I'm doing something wrong?
- How to handle exceptions, which throw the functions FunctionAsync()?
- How to count the number of the finished tasks so that I can be sure that all tasks are completed and I can proceed further?
Thanks in advance.