Async and await file processing
With acync and await, developers can keep their applications fully responsive while performing intensive tasks such as file processing. This makes a perfect candidate for using asynchronous code. If you have several large files that need copying, an async and await method would be a perfect solution for keeping your form responsive.
Getting ready
Ensure that you have added the following using
statements to the top of your code file:
using System.IO; using System.Threading;
For asynchronous code to work, we need to include the threading namespaces.
How to do it...
- Create two folders called
AsyncDestination
andAsyncSource
:

- To the
AsyncSource
folder, add a couple of large files to process:

- Create a new WinForms application and add a forms times control, a button, and a label called
lblTimer
to the form. Call the timerasyncTimer
and set itsInterval
to1000
milliseconds (1 second):

- In the code above the constructor, add the
CancellationTokenSource
object and theelapsedTime...