Updating textbox asynchronously

08-Sep-2019 14:08

Before you begin this tutorial, you should have a solid base knowledge of all C#'s fundamental concepts, and have at least a general knowledge of the different API's available for performing multithreaded and asynchronous operations in C#.

The examples are kept very very simple, but basic knowledge is still required So, what has brought this on?

Well, the representing the operation is returned to the UI, so it has access to it, but how do we get the result? We register a callback that is run when the operation completes (this is essentially what we were doing with the object to capture the UI's synchronisation context so we could update the UI from the UI thread. It is saying, 'run this callback (represented by the lambda passed as the first arguement to class, as you can reuse previous threads that are sitting in the thread pool, instead of creating a brand new thread every time (which is expensive)).

In fact, the TPL (Task Parallel Library), of which Tasks are central to, is currently the only API that makes use of certain optimisations to the thread pool that the CLR team made.

However, Tasks provide a number of beneficial, easy to use features that stand it apart from to the caller.

The gist of the point was that rather than have the background thread directly update the UI directly using , you should try and favour having the background thread raise an event (of which contains the data used to update the UI), have the form subscribe to this event, and thus have the form update itself when the data from the background thread becomes available.

This would mean that the operation knows nothing of the UI, making the program more maintainable, and the operation itself reusable in the future.

For example, if you have a loop that has many iterations (1000 ), and each iteration fires up a 2000 threads (this is always subject to change by Microsoft).

You can change that, but it isn't usually a great idea to do so).If you simply raise an event (in the standard, everyday way) from the background thread, and have the form handle the event, the handler method will be run on that background thread, thus meaning you would still have to invoke UI updates onto the main thread.

As I revisit 2008 and consider my approaching birthday a couple months from now (Pisces), I’ve realized I’ve come a long way over the X # years I’ve been on this earth.… continue reading »

Read more

Most of the early postings were submitted by Newmark and were notices of social events of interest to software and Internet developers living and working in the San Francisco Bay Area. The number of subscribers and postings grew rapidly.… continue reading »

Read more