concept asynchronous operation in category .net

appears as: synchronous operations, n asynchronous operation, asynchronous operation, asynchronous operations, n asynchronous operations, The asynchronous operation, Asynchronous operations
Concurrency in .NET

This is an excerpt from Manning's book Concurrency in .NET.

  • How to combine asynchronous operations with the Task Parallel Library
  • We’ll start by looking at how to manage exceptions in a functional style, with a focus on asynchronous operations. Next, we’ll explore functional combinators, a useful programming tool for building a set of utility functions that allow you to create complex functions by composing smaller and more concise operators. These combinators and techniques make your code more maintainable and performant, improving your ability to write concurrent computations and handle side effects. Toward the end of this chapter, we’ll go through how to interop between C# and F# by calling and passing asynchronous functions from one to the other.

    Listing 10.12 AsyncResult handler to catch and wrap asynchronous computations
    module Result =
     let ofChoice value =   #1  
                match value with
                | Choice1Of2 value -> Ok value
                | Choice2Of2 e -> Error e
    
    module AsyncResult = 
          let handler (operation:Async<'a>) : AsyncResult<'a> = async {
     let! result = Async.Catch operation   #2  
    
     return (Result.ofChoice result) }   #3  
    
    #1   Maps the function from the Choice DU, which is the returned type of the Async.Catch operator, to the Result type DU cases. This map function was defined in chapter 9.
    #2   Runs the asynchronous operation using the Async.Catch operator to safeguard against possible error
    #3   The output of the function is mapped in favor of the Result type.
    C# in Depth

    This is an excerpt from Manning's book C# in Depth.

    A few patterns in .NET are easily identifiable by the names of the methods involved—BeginXXX and EndXXX suggest an asynchronous operation, for example. The TryXXX pattern is one that has had its use expanded from .NET 1.1 to 2.0. It’s designed for situations that might normally be considered to be errors (in that the method can’t perform its primary duty), but where failure could well occur without indicating a serious issue, and shouldn’t be deemed exceptional. For instance, users often fail to type in numbers correctly, so being able to try to parse some text without having to catch an exception and swallow it is useful. Not only does it improve performance in the failure case, but more importantly, it saves exceptions for genuine error cases where something is wrong in the system (however widely you wish to interpret that). It’s a useful pattern to have up your sleeve as a library designer, when applied appropriately.

  • The asynchronous operation is HttpClient.GetStringAsync.
  • // Warning: these don't really exist
    // Imaginary interfaces for asynchronous operations returning values
    public interface IAwaitable<T>
    {
       IAwaiter<T> GetAwaiter();
    }
    
    public interface IAwaiter<T> : INotifyCompletion
    {
       bool IsCompleted { get; }
       T GetResult();
    
       // Inherited from INotifyCompletion
       // void OnCompleted(Action continuation);
    }
    
    // Imaginary interfaces for "void" asynchronous operations
    public interface IAwaitable
    {
       IAwaiter GetAwaiter();
    }
    
    public interface IAwaiter : INotifyCompletion
    {
       bool IsCompleted { get; }
       void GetResult();
    
       // Inherited from INotifyCompletion
       // void OnCompleted(Action continuation);
    }

    Of course, things don’t always work smoothly, and the idiomatic way of representing failures in .NET is via exceptions. Like returning a value to the caller, exception handling requires extra support from the language. When you want to throw an exception, the original caller of the async method may not be on the stack; and when you await an asynchronous operation that’s failed, it may well not have executed on the same thread, so you need a way of marshaling the failure across. If you think of failure as just another kind of result, it makes sense that exceptions and return values are handled similarly.

    In this section we’ll look at how exceptions cross over both of the boundaries in figure 15.1. Let’s start with the boundary between the async method and the asynchronous operation it’s awaiting.