KEMBAR78
Asynchronous programming in C# | PPTX
Asynchronous
programming in C#
BOHDAN PASHKOVSKYI
CORECAMP Ivano-Frankivsk 2017
Multi-threading vs Asynchronous
Here, there are two completely different concepts involved, First –
Synchronous and Asynchronous programming model and second –
Single threaded and multi-threaded environments. Each
programming model (Synchronous and Asynchronous) can run in
single threaded and multi-threaded environment.
CORECAMP Ivano-Frankivsk 2017
CORECAMP Ivano-Frankivsk 2017
CORECAMP Ivano-Frankivsk 2017
Synchronous Programming model – In this programming model, A thread is assigned to one task and starts
working on it. Once the task completes then it is available for the next task. In this model, it cannot leave the
executing task in mid to take up another task. Let’s discuss how this model works in single and multi-threaded
environments.
Single Threaded – If we have couple of tasks to be worked on and the current system provides just a single thread,
then tasks are assigned to the thread one by one. It can be pictorially depicted as
CORECAMP Ivano-Frankivsk 2017
Multi-Threaded – In this environment, we used to have multiple threads which can take up these tasks and start
working on that. It means we have a pool of threads (new threads can also be created based on the requirement
and available resources) and bunch of tasks. So these thread can work on these as
CORECAMP Ivano-Frankivsk 2017
Asynchronous Programming Model – In contrary to Synchronous programming model, here a thread once start executing a
task it can hold it in mid, save the current state and start executing another task.
CORECAMP Ivano-Frankivsk 2017
If our system is capable of having multiple threads then all the threads can work in asynchronous model as well
CORECAMP Ivano-Frankivsk 2017
So till now we have discussed four scenarios –
1. Synchronous Single Threaded
2. Synchronous Multi-Threaded
3. Asynchronous Single Threaded
4. Asynchronous Multi-Threaded
CORECAMP Ivano-Frankivsk 2017
private void StartButtonClick(object sender, RoutedEventArgs e)
{
string text = Download();
TextBox.Text = text;
}
private string Download()
{
return new WebClient().DownloadString("http://corecamp.net/");
}
Synchronous method in Desktop application
CORECAMP Ivano-Frankivsk 2017
private async void StartButtonClick(object sender, RoutedEventArgs e)
{
string text = await Download();
TextBox.Text = text;
}
private async Task<string> Download()
{
return await new WebClient().DownloadStringTaskAsync("http://corecamp.net/");
}
Asynchronous method in Desktop application
CORECAMP Ivano-Frankivsk 2017
Evolution of Asynchronous concepts
• Asynchronous Programming Model (APM)
• Evented Asynchronous Programming (EAP)
• Task-based Asynchronous Programming (TAP)
CORECAMP Ivano-Frankivsk 2017
Asynchronous Programming Model (APM)
// .NET 1 model
file.BeginRead(buffer, 0, maxLength, asyncResult => {
int numBytesRead = files.EndRead(asyncResult);
// Now do something with "buffer"
}, null);
CORECAMP Ivano-Frankivsk 2017
Evented Asynchronous Programming (EAP)
// .NET 2 model
webClient.DownloadStringCompleted += (sender, args) => {
string html = args.Result;
// Now do someting with "html"
};
webClient.DownloadStringAsync(new Uri("http://example.com"));
CORECAMP Ivano-Frankivsk 2017
Task-based Asynchronous Programming (TAP)
Task<string> htmlTask = webClient.DownloadStringTaskAsync(url);
1. string html = htmlTask.Result; // Sync (block until done)
2. htmlTask.ContinueWith(task => {
string html = task.Result; // Async, C# 4
});
3. string html = await htmlTask; // Async. C# 5
CORECAMP Ivano-Frankivsk 2017
DEMO
Thank you for the
attention!

Asynchronous programming in C#

  • 1.
  • 2.
    CORECAMP Ivano-Frankivsk 2017 Multi-threadingvs Asynchronous Here, there are two completely different concepts involved, First – Synchronous and Asynchronous programming model and second – Single threaded and multi-threaded environments. Each programming model (Synchronous and Asynchronous) can run in single threaded and multi-threaded environment.
  • 3.
  • 4.
  • 5.
    CORECAMP Ivano-Frankivsk 2017 SynchronousProgramming model – In this programming model, A thread is assigned to one task and starts working on it. Once the task completes then it is available for the next task. In this model, it cannot leave the executing task in mid to take up another task. Let’s discuss how this model works in single and multi-threaded environments. Single Threaded – If we have couple of tasks to be worked on and the current system provides just a single thread, then tasks are assigned to the thread one by one. It can be pictorially depicted as
  • 6.
    CORECAMP Ivano-Frankivsk 2017 Multi-Threaded– In this environment, we used to have multiple threads which can take up these tasks and start working on that. It means we have a pool of threads (new threads can also be created based on the requirement and available resources) and bunch of tasks. So these thread can work on these as
  • 7.
    CORECAMP Ivano-Frankivsk 2017 AsynchronousProgramming Model – In contrary to Synchronous programming model, here a thread once start executing a task it can hold it in mid, save the current state and start executing another task.
  • 8.
    CORECAMP Ivano-Frankivsk 2017 Ifour system is capable of having multiple threads then all the threads can work in asynchronous model as well
  • 9.
    CORECAMP Ivano-Frankivsk 2017 Sotill now we have discussed four scenarios – 1. Synchronous Single Threaded 2. Synchronous Multi-Threaded 3. Asynchronous Single Threaded 4. Asynchronous Multi-Threaded
  • 10.
    CORECAMP Ivano-Frankivsk 2017 privatevoid StartButtonClick(object sender, RoutedEventArgs e) { string text = Download(); TextBox.Text = text; } private string Download() { return new WebClient().DownloadString("http://corecamp.net/"); } Synchronous method in Desktop application
  • 11.
    CORECAMP Ivano-Frankivsk 2017 privateasync void StartButtonClick(object sender, RoutedEventArgs e) { string text = await Download(); TextBox.Text = text; } private async Task<string> Download() { return await new WebClient().DownloadStringTaskAsync("http://corecamp.net/"); } Asynchronous method in Desktop application
  • 12.
    CORECAMP Ivano-Frankivsk 2017 Evolutionof Asynchronous concepts • Asynchronous Programming Model (APM) • Evented Asynchronous Programming (EAP) • Task-based Asynchronous Programming (TAP)
  • 13.
    CORECAMP Ivano-Frankivsk 2017 AsynchronousProgramming Model (APM) // .NET 1 model file.BeginRead(buffer, 0, maxLength, asyncResult => { int numBytesRead = files.EndRead(asyncResult); // Now do something with "buffer" }, null);
  • 14.
    CORECAMP Ivano-Frankivsk 2017 EventedAsynchronous Programming (EAP) // .NET 2 model webClient.DownloadStringCompleted += (sender, args) => { string html = args.Result; // Now do someting with "html" }; webClient.DownloadStringAsync(new Uri("http://example.com"));
  • 15.
    CORECAMP Ivano-Frankivsk 2017 Task-basedAsynchronous Programming (TAP) Task<string> htmlTask = webClient.DownloadStringTaskAsync(url); 1. string html = htmlTask.Result; // Sync (block until done) 2. htmlTask.ContinueWith(task => { string html = task.Result; // Async, C# 4 }); 3. string html = await htmlTask; // Async. C# 5
  • 16.
  • 17.
    Thank you forthe attention!