What are some strategies for working with multiple threads in libraries?

I am working on a library that deals with long-term tasks such as downloading files and processing large amounts of text. I want this multi-threaded library so that these tasks do not freeze the applications that use them.

Do you have any tips for this in a structured way or specific classes that I should use / avoid? I was thinking about using the IAsyncResult interface: http://msdn.microsoft.com/en-us/library/system.iasyncresult.aspx or maybe some BackgroundWorkers.

+4
source share
1 answer

so that these tasks do not freeze the applications that use them.

If this is your goal, you should study the standard asynchronous programming patterns within the framework.

If your library is targeting .NET 4, return it to Task and Task<T> , as this will make it easier to switch to async support in the next version of C # and VB.NET. It also has a very nice addition allowing you to use synchronous use without any additional work on your part, as the user can always just do:

 var result = foo.BarAsync().Result; // Getting Task<T>.Result blocks, effectively making this synchronous 

If you plan to use .NET 3.5 or earlier, you should consider using an asynchronous event-based template , as it is used in more existing APIs than APM.

+4
source

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


All Articles