I’m writing a FTPS-Framework. The user (programmer that uses my framework) should can download a file async (in another Thread). So, my big problem is, that the programmer cannot handle the Exceptions throwed inside the DownloadFile-Method in the transfer-Thread:
– When I demand an AsyncCallback for telling the program that the download transfer is finish, how can “he” handling Exceptions (from his Thread)?
– The BackgroundWorker has the same problem, furthermore the programmer have to give me a RunWorkerCompletedEventHandler(object, RunWorkerCompletedEventArgs)
Thanks!
There have been two options for exposing an async API since .NET 2.0:
The Asynchronous Programming Model – favoured for more backend / library code.
The Event-based Asynchronous Pattern – favoured when your code will be used in a UI.
In .NET 4, you now also have the “Task-based Asynchronous Pattern” ( TAP ):
Simplify Asynchronous Programming with Tasks
If you can, I would choose the TAP – it is the best of the three and solves problems like exception handling.