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
AsyncDestinationandAsyncSource:

- To the
AsyncSourcefolder, 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
lblTimerto the form. Call the timerasyncTimerand set itsIntervalto1000milliseconds (1 second):

- In the code above the constructor, add the
CancellationTokenSourceobject and theelapsedTime...