Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Asynchronous call of several async functions and exception handling

$
0
0

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:

  1. Will this work, or I'm doing something wrong?
  2. How to handle exceptions, which throw the functions FunctionAsync()?
  3. 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.


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>