Event-Based Asynchrony and Callbacks
A much better approach is moving the long-running operation into a separate thread, so that the UI can remain responsive while the other thread executes the operation. Lots of classes in the .NET Framework, especially those whose job is interacting with the Web and with networks-expose event-based asynchrony through methods that launch and execute an operation on a separate thread and raise an event when it is completed, passing the result to the caller via a callback. The
WebClient class has an asynchronous counterpart of
DownloadStringAsync, that you can use to execute the code on a separate thread and wait for the query result via a callback. The following code demonstrates how to ...