ASP.Net, ADO, MS AccessC# How to use asychronous tasks to return results

C# How to use asychronous tasks to return results

(42 hits)

Hits=15 When the c# compiler encounters an await in an async method, it reformats it into a task.Run on the same thread as the async method. Now, there is a problem updating the User Interface because they share the same thread. Using the await on a code will create a continuation block for completion after the async code is done. You won't get a "marshalled from a different thread" error by using await with async or using continueWith and putting your UI update code in the continueWith code block.

ContinueWith

The ContinueWith is execute by the async thread doing work. The Task<TResult> is return by the async function. The value of the async method can by assessed by the Result variable.

ConfigureAwait

Hits=18 await function().ConfigureAwait(false) will run the method on a separate thread from the UI thread. The await call will instantaneous return control back to the UI thread. In the code example, the doLongRunningFnc1 processes a 100 numbers raise to the third power without blocking the UI thread.