Asynchronous programming patterns - .NET (original) (raw)

.NET provides three patterns for performing asynchronous operations:

Comparison of patterns

For a quick comparison of how the three patterns model asynchronous operations, consider a Read method that reads a specified amount of data into a provided buffer starting at a specified offset:

public class MyClass  
{  
    public int Read(byte [] buffer, int offset, int count);  
}  

The TAP counterpart of this method would expose the following single ReadAsync method:

public class MyClass  
{  
    public Task<int> ReadAsync(byte [] buffer, int offset, int count);  
}  

The EAP counterpart would expose the following set of types and members:

public class MyClass  
{  
    public void ReadAsync(byte [] buffer, int offset, int count);  
    public event ReadCompletedEventHandler ReadCompleted;  
}  

The APM counterpart would expose the BeginRead and EndRead methods:

public class MyClass  
{  
    public IAsyncResult BeginRead(  
        byte [] buffer, int offset, int count,
        AsyncCallback callback, object state);  
    public int EndRead(IAsyncResult asyncResult);  
}  

See also