You might have a code that needs to download data from internet or access to a file on your local drive.. These kind of processes extends the response time of your software and you may want to take caution for that. Asynchronous Programming is the methodology to set the run time more responsive.
An example code is shown below (source: http://msdn.microsoft.com/en-us/library/system.io.stream(v=vs.110).aspx) Here the method is defined as async because loading files on local drive may take time.
An example code is shown below (source: http://msdn.microsoft.com/en-us/library/system.io.stream(v=vs.110).aspx) Here the method is defined as async because loading files on local drive may take time.
private async void Button_Click(object sender, RoutedEventArgs e)
{
string StartDirectory = @"c:\Users\exampleuser\start";
string EndDirectory = @"c:\Users\exampleuser\end";
foreach (string filename in Directory.EnumerateFiles(StartDirectory))
{
using (FileStream SourceStream = File.Open(filename, FileMode.Open))
{
using (FileStream DestinationStream = File.Create(EndDirectory + filename.Substring(filename.LastIndexOf('\\'))))
{
await SourceStream.CopyToAsync(DestinationStream);
}
}
}
}
Good resources on MSDN:
Asynchronous Programming with Async and Await (C# and Visual Basic)
http://msdn.microsoft.com/en-us/library/hh191443.aspx
Calling Synchronous Methods Asynchronously
http://msdn.microsoft.com/en-us/library/2e08f6yc(v=vs.110).aspx
async (C# Reference)
http://msdn.microsoft.com/en-us/library/hh156513.aspx