Writing asynchronous code
Before we discuss the code in async way, lets first discuss our normal code that is nothing but a synchronous code, let's consider following code snippet:
public class FilePolling { public void PoleAFile(string fileName) { Console.Write($"This is polling file: {fileName}"); //file polling stuff goes here } }
The preceding code snippet is short and sweet. It tells us it is polling to a specific file. Here system has to wait to complete the operation of poling a file before it start next. This is what synchronous code is. Now, consider a scenario where we need not wait to complete the operation of this function to start another operation or function. To meet such scenarios, we have asynchronous coding, this is possible with the keyword, async
.
Consider following code:
public async void PoleAFileAsync(string fileName) { Console.Write($"This is polling file: {fileName}"); //file polling async stuff goes here }
Just...