.netmultithreadingclass-libraryresponsibility

.Net Class Libraries: Threading responsibility


I am working on a class library and one of the classes is responsible for retrieving an Xml file using XDocument.Load(url) from the internet. Seeing as this operation could take a few seconds to complete, it makes sense to run it on it's own thread.

Who's responsibility is it to create this thread? The consumer or the class that retrieves the file? Is there a best practice regarding this?


Solution

  • The best practice is to implement an async pattern. This means that if your class has a LoadXmlmethod you also implement an LoadXmlAsync method and some kind of OnCompleted event.

    You can read about it here