![]() ![]() Here, we are using async programming to read all the contents from the file, so it will not wait to get a return value from this method and execute the other lines of code. In our sampleText.txt, the file contains too many characters, so It will take a long time to read all the characters. In the code given above, we are calling a ReadFile method to read the contents of a text file and get the length of the total characters present in the text file. String s = await reader.ReadToEndAsync() Ĭonsole.WriteLine(" File reading is completed") Reads all characters from the current position to the end of the stream asynchronously Using (StreamReader reader = new StreamReader(file)) In this example, we are going to take two methods, which are not dependent on each other.įor (int i = 0 i task = ReadFile(filePath) Ĭonsole.WriteLine(" Total length: " + length) Ĭonsole.WriteLine(" File reading is stating") We are going to take a console application for our demonstration. Let’s start with practical examples for understanding the programming concept. To come out of this problem, we have to write too many codes in traditional programming, but if we use the async and await keywords, we will get the solutions in much less code.Īlso, we are going to see more examples, and if any third Method, as Method3 has a dependency of method1, then it will wait for the completion of Method1 with the help of await keyword.Īsync and await in C# are the code markers, which marks code positions from where the control should resume after a task completes. We can run all the methods parallelly by using simple thread programming, but it will block UI and wait to complete all the tasks. Thus, it will be a time-intensive process even though both methods are not depending on each other. In Synchronous programming, it will execute the first Method1 and it will wait for the completion of this method, and then it will execute Method2. ![]() Suppose we are using two methods as Method1 and Method2 respectively, and both the methods are not dependent on each other, and Method1 takes a long time to complete its task. We will get all the benefits of traditional Asynchronous programming with much less effort with the help of async and await keywords. By using Asynchronous programming, the Application can continue with the other work that does not depend on the completion of the entire task. In other words, if any process is blocked in a synchronous application, the whole application gets blocked, and our application stops responding until the whole task completes.Īsynchronous programming is very helpful in this condition. When we are dealing with UI, and on button click, we use a long-running method like reading a large file or something else which will take a long time, in that case, the entire application must wait to complete the whole task. Nowadays, Asynchronous programming is very popular with the help of the async and await keywords in C#. ![]() In this article, you'll learn what C# async and C# await keywords are and how to use async and await in C# code. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |