- C# Asynchronous Programming
- C# Asynchronous Program 1
- C# Asynchronous Program 2
- C# Asynchronous Program 3
The post C# Introduction lists all of the C# series of posts we have at this site.
What is the difference between the synchronous and asynchronous execution model? In a synchronous model, the program is executed line by line, one at a time. When a function is called, the program execution has to wait until the function returns before it can do anything else and continue execution to the next line.
Suppose you have a function that is very time consuming. In this case you might want to consider sing asynchronous programming. When we do this we provide a callback function to the time consuming process. This callback function will be executed when the time consuming process is finished. In this way the program is more UI responsive.
Examples of Asynchronous Programs
Two program examples are a media player and a Web browser. As the media player is playing a movie or a song, the UI continues to be responsive. You can re-size the window for example. Without asynchronous programming, the window (GUI) would freeze while the file was playing in the media player. We can use asynchronous programming when we are accessing the Web, working with files or databases or working with images. Each of these take time.
How?
Before .NET 4.5 we could use two common approaches: multithreading or callbacks. Both of these are complex. Since .NET 4.5 we can use Asynch and Await keywords.
The book Illustrated C# 7, Fifth Edition by Daniel Solis and Cal Schrotenboer published by Apress discusses and defines asynchrony by first discussing the differences between a process and a thread, starting on page 566. Later in this series of posts we will create a WPF program to illustrate the basics of asynchronous programming. Before doing that we will first look at processes and threads.