Giter Site home page Giter Site logo

chsandeepkumar / async-workshop-2022 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jeremybytes/async-workshop-2022

0.0 1.0 0.0 1.34 MB

Slides, code samples, and hands-on labs for the "Understanding Asynchronous Programming in C#" workshop (2022)

License: MIT License

JavaScript 0.49% C# 84.55% CSS 4.93% HTML 10.03%

async-workshop-2022's Introduction

Understanding Asynchronous Programming in C# [2022 Workshop]

This project contains slides and code samples for the "Understanding Asynchronous Programminc in C#" workshop with Jeremy Clark.

Overview and Objectives

Level: Introductory / Intermediate
Asynchronous code is everywhere. In our C# code, we "await" method calls to services and databases; and more and more packages that we use every day have asynchronous methods. But do you really understand what this does?

Understanding is critical. When done correctly, we can make our applications more responsive, faster, and reliable. When done incorrectly, we can block threads or even hang the application entirely.

In this half-day workshop, we'll start at the beginning to see how "await" relates to "Task”. We'll do this by calling an asynchronous method, getting a result, and handling errors that come up. We will create our own "awaitable" methods to see how Task and return types work together. With our own methods, we'll also better understand why we may (or may not) care about getting back to the original calling thread. We'll also cover some dangers, such as using "async void" or misusing ".Result". Finally, we'll use Task to run multiple operations in parallel to make things faster. With all of these skills, we can write more effective asynchronous code.

You will learn:

  • How to use "await" and "Task" to run asynchronous methods
  • About handling errors from asynchronous processes
  • About writing your own asynchronous methods How to avoid pitfalls such as "async void" and ".Result"
  • About running multiple methods in parallel

Pre-Requisites
For this workshop, it is assumed that you have some experience with C#, but no specific asynchronous programming experience is needed. To run the sample code, you will need .NET 6.0 installed. Jeremy will be using Visual Studio 2022, but the code samples will run using Visual Studio Code or the editor of your choice.

Running the Samples

The sample code uses .NET 6. The console and web samples will run on all Window, macOS, and Linux versions that support .NET 6. The desktop samples are Windows-only.

Samples have been tested with Visual Studio 2022 and Visual Studio Code.

All samples require the "Person.Service" web service be running. To start the service, navigate to the "Person.Service" folder from the command line and type "dotnet run".

Ex:

C:\understanding-async\People.Service> dotnet run

The service can be found at http://localhost:9874/people

Project Layout

The "DemoCode" folder contains the code samples used in the workshop.
Shared Projects

  • People.Service
    A web service that supplies data for the sample projects.
  • TaskAwait.Shared
    A library with data types that are shared across projects (primarily the "Person" type).
  • TaskAwait.Library
    A library with asynchronous methods that access the web service. These methods are called in the various applications detailed below.
    Relevant file: PersonReader.cs

Concurrent Samples
The Concurrent samples run asynchronous methods, get results, handle exceptions, and support cancellation (unless otherwise noted).

  • Concurrent.UI.Console
    A console application (Windows, macOS, Linux)
    Relevant file: Program.cs
  • Concurrent.UI.Desktop
    A WPF desktop application (Windows only).
    Relevant file: MainWindow.xaml.cs
  • Concurrent.UI. Web
    A web application (Windows, macOS, Linux).
    Note: this application does not support cancellation.
    Relevant file: Controllers/PeopleController.cs

Parallel Samples
The Parallel samples use Task to run asynchronous methods in parallel - also get results, handle exceptions, and support cancellation (unless otherwise noted).

  • Parallel.Basic
    A console application that does not support cancellation or error handling (Windows, macOS, Linux).
    Relevant file: Program.cs
  • Parallel.UI.Console
    A console application (Windows, macOS, Linux).
    Relevant file: Program.cs
  • Parallel.UI.Desktop
    A WPF desktop application (Windows only).
    Relevant file: MainWindow.xaml.cs
  • Parallel.UI. Web
    A web application (Windows, macOS, Linux).
    Note: this application does not support cancellation.
    Relevant file: Controllers/PeopleController.cs

Progress Reporting (Bonus Material)
The Progress Reporting samples show how to report progress from an asynchronous method - in this case, as a percentage complete. These also get results, handle exceptions, and support cancellation.

  • ProgressReport.UI.Console
    A console application that reports percentage complete progress through text. Ex: "21% Complete". (Windows, macOS, Linux)
    Relevant file: Program.cs
  • Parallel.UI.Desktop
    A WPF desktop application that reports percentage complete progress through a graphical progress bar. (Windows only)
    Relevant file: MainWindow.xaml.cs
  • TaskAwait.Library
    This shared library contains a method that supports progress reporting.
    Relevant method:
public async Task<List<Person>> GetPeopleAsync(IProgress<int> progress,
    CancellationToken cancelToken = new CancellationToken()) {...}

Hands-On Labs (Bonus Material)

The "Labs" folder contains hands-on labs. These labs are not directly covered in the workshop due to time constraints but are provided as additional resources that can be completed separately.

Each lab consists of the following:

  • Labxx-Instructions (Markdown)
    A markdown file containing the lab instructions. This includes the scenario, a set of goals, and step-by-step instructions.
    This can be viewed on GitHub or in Visual Studio Code (just click the "Open Preview to the Side" button in the upper right corner).

  • Starter (Folder)
    This folder contains the starting code for the lab.

  • Completed (Folder)
    This folder contains the completed solution. If at any time, you get stuck during the lab, you can check this folder for a solution.

Additional Resources

Related Articles (by Jeremy)

Video Series & Articles (by Jeremy)
Each of these has a lot of supporting links:

Other Resources
Stephen Cleary has lots of great articles, books, and practical advice.

Stephen Toub has great articles, too (generally with advanced insights).

Articles / Videos Suggested by prior Workshop Attendees

For more information, visit jeremybytes.com.

async-workshop-2022's People

Contributors

jeremybytes avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.