C#idiomatic C#
tpl-and-traditional-async-programmingstandard/parallel-programming

Async and concurrency

TPL and traditional .NET asynchronous programming

Learn more about: TPL and traditional .NET asynchronous programming

  • page standard/parallel-programming/tpl-and-traditional-async-programming
  • updated 03/30/2017

.NET provides the following two standard patterns for performing I/O-bound and compute-bound asynchronous operations:

The Task Parallel Library (TPL) can be used in various ways in conjunction with either of the asynchronous patterns. You can expose both APM and EAP operations as Task objects to library consumers, or you can expose the APM patterns but use Task objects to implement them internally. In both scenarios, by using Task objects, you can simplify the code and take advantage of the following useful functionality:

Wrap APM operations in a Task

Both the System.Threading.Tasks.TaskFactory and System.Threading.Tasks.TaskFactory classes provide several overloads of the System.Threading.Tasks.TaskFactory.FromAsync and System.Threading.Tasks.TaskFactory.FromAsync methods that let you encapsulate an APM begin/end method pair in one System.Threading.Tasks.Task or System.Threading.Tasks.Task instance. The various overloads accommodate any begin/end method pair that have from zero to three input parameters.

For pairs that have End methods that return a value (a Function in Visual Basic), use the methods in System.Threading.Tasks.TaskFactory that create a System.Threading.Tasks.Task. For End methods that return void (a Sub in Visual Basic), use the methods in System.Threading.Tasks.TaskFactory that create a System.Threading.Tasks.Task.

For those few cases in which the Begin method has more than three parameters or contains ref or out parameters, additional FromAsync overloads that encapsulate only the End method are provided.

The following example shows the signature for the FromAsync overload that matches the System.IO.FileStream.BeginRead and System.IO.FileStream.EndRead methods.

sample · csharpsamples/snippets/csharp/VS_Snippets_Misc/fromasync/cs/fromasync.cs01

csharp
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net;

namespace specialNamespaceForOneMethodSignature
{
    using System;
    class Task<TResult>
    {
        public TResult TValue;
    }
    class Dummy <TResult>
    {
            // <snippet01>
            public Task<TResult> FromAsync<TArg1, TArg2, TArg3>(
                Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, //BeginRead
                 Func<IAsyncResult, TResult> endMethod, //EndRead
                 TArg1 arg1, // the byte[] buffer
                 TArg2 arg2, // the offset in arg1 at which to start writing data
                 TArg3 arg3, // the maximum number of bytes to read
                 object state // optional state information
                )
                //</snippet01>
        { return new Task<TResult>(); }
    }
}

namespace APM_Task
{
    class TestFileStreamNaive
    {
        //<snippet02>
        public string GetFileData(string path)
        {
            FileInfo fi = new FileInfo(path);
            byte[] data = new byte[fi.Length];
            FileStream fs;

            // a using statement is ok here because we Wait before returning
            // and will be done with the filestream.
            using (fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true))
            {
                //Task<int> returns the number of bytes read
                Task<int> task = Task<int>.Factory.FromAsync(
                        fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

                task.Wait();
                return new UTF8Encoding().GetString(data);
            }
        }
        //</snippet02>
        static void Main()
        {
            TestFileStreamNaive instance = new TestFileStreamNaive();
            string s = instance.GetFileData("testfile.txt");
        }
    }

    class AsyncResult : IAsyncResult
    {

        public AsyncResult() { }
        public object AsyncState
        {
            get { return "Hello from a parallel universe."; }
        }

        public WaitHandle AsyncWaitHandle
        {
            get { throw new NotImplementedException(); }
        }

        public bool CompletedSynchronously
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsCompleted
        {
            get { throw new NotImplementedException(); }
        }

        static IAsyncResult DoSomethingAsynchronously()
        {
            return new AsyncResult();
        }

        //<snippet07>
        static Task<String> ReturnTaskFromAsyncResult()
        {
            IAsyncResult ar = DoSomethingAsynchronously();
            Task<String> t = Task<string>.Factory.FromAsync(ar, _ =>
                {
                    return (string)ar.AsyncState;
                });

            return t;
        }
        //</snippet07>
    }

    //<snippet08>
    class WebDataDownloader
    {

        static void Main()
        {
            WebDataDownloader downloader = new WebDataDownloader();
            string[] addresses = { "http://www.msnbc.com", "http://www.yahoo.com",
                                     "http://www.nytimes.com", "http://www.washingtonpost.com",
                                     "http://www.latimes.com", "http://www.newsday.com" };
            CancellationTokenSource cts = new CancellationTokenSource();

            // Create a UI thread from which to cancel the entire operation
            Task.Factory.StartNew(() =>
            {
                Console.WriteLine("Press c to cancel");
                if (Console.ReadKey().KeyChar == 'c')
                    cts.Cancel();
            });

            // Using a neutral search term that is sure to get some hits.
            Task<string[]> webTask = downloader.GetWordCounts(addresses, "the", cts.Token);

            // Do some other work here unless the method has already completed.
            if (!webTask.IsCompleted)
            {
                // Simulate some work.
                Thread.SpinWait(5000000);
            }

            string[] results = null;
            try
            {
                results = webTask.Result;
            }
            catch (AggregateException e)
            {
                foreach (var ex in e.InnerExceptions)
                {
                    OperationCanceledException oce = ex as OperationCanceledException;
                    if (oce != null)
                    {
                        if (oce.CancellationToken == cts.Token)
                        {
                            Console.WriteLine("Operation canceled by user.");
                        }
                    }
                    else
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            finally
            {
                cts.Dispose();
            }
            if (results != null)
            {
                foreach (var item in results)
                    Console.WriteLine(item);
            }
            Console.ReadKey();
        }

        Task<string[]> GetWordCounts(string[] urls, string name, CancellationToken token)
        {
            TaskCompletionSource<string[]> tcs = new TaskCompletionSource<string[]>();
            WebClient[] webClients = new WebClient[urls.Length];

            // If the user cancels the CancellationToken, then we can use the
            // WebClient's ability to cancel its own async operations.
            token.Register(() =>
                {
                    foreach (var wc in webClients)
                    {
                        if (wc != null)
                            wc.CancelAsync();
                    }
                });

            object m_lock = new object();
            int count = 0;
            List<string> results = new List<string>();
            for (int i = 0; i < urls.Length; i++)
            {
                webClients[i] = new WebClient();

                #region callback
                // Specify the callback for the DownloadStringCompleted
                // event that will be raised by this WebClient instance.
                webClients[i].DownloadStringCompleted += (obj, args) =>
                 {
                     if (args.Cancelled == true)
                     {
                         tcs.TrySetCanceled();
                         return;
                     }
                     else if (args.Error != null)
                     {
                         // Pass through to the underlying Task
                         // any exceptions thrown by the WebClient
                         // during the asynchronous operation.
                         tcs.TrySetException(args.Error);
                         return;
                     }
                     else
                     {
                         // Split the string into an array of words,
                         // then count the number of elements that match
                         // the search term.
                         string[] words = null;
                         words = args.Result.Split(' ');
                         string NAME = name.ToUpper();
                         int nameCount = (from word in words.AsParallel()
                                          where word.ToUpper().Contains(NAME)
                                          select word)
                                         .Count();

                         // Associate the results with the url, and add new string to the array that
                         // the underlying Task object will return in its Result property.
                         results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, name));
                     }

                     // If this is the last async operation to complete,
                     // then set the Result property on the underlying Task.
                     lock (m_lock)
                     {
                         count++;
                         if (count == urls.Length)
                         {
                             tcs.TrySetResult(results.ToArray());
                         }
                     }
                 };
                #endregion

                // Call DownloadStringAsync for each URL.
                Uri address = null;
                try
                {
                    address = new Uri(urls[i]);
                    // Pass the address, and also use it for the userToken
                    // to identify the page when the delegate is invoked.
                    webClients[i].DownloadStringAsync(address, address);
                }

                catch (UriFormatException ex)
                {
                    // Abandon the entire operation if one url is malformed.
                    // Other actions are possible here.
                    tcs.TrySetException(ex);
                    return tcs.Task;
                }
            }

            // Return the underlying Task. The client code
            // waits on the Result property, and handles exceptions
            // in the try-catch block there.
            return tcs.Task;
        }
        //</snippet08>
    }

    #region snippet09
    class Calculator
    {
        public IAsyncResult BeginCalculate(int decimalPlaces, AsyncCallback ac, object state)
        {
            Console.WriteLine($"Calling BeginCalculate on thread {Thread.CurrentThread.ManagedThreadId}");
            Task<string> f = Task<string>.Factory.StartNew(_ => Compute(decimalPlaces), state);
            if (ac != null) f.ContinueWith((res) => ac(f));
            return f;
        }

        public string Compute(int numPlaces)
        {
            Console.WriteLine($"Calling compute on thread {Thread.CurrentThread.ManagedThreadId}");

            // Simulating some heavy work.
            Thread.SpinWait(500000000);

            // Actual implementation left as exercise for the reader.
            // Several examples are available on the Web.
            return "3.14159265358979323846264338327950288";
        }

        public string EndCalculate(IAsyncResult ar)
        {
            Console.WriteLine($"Calling EndCalculate on thread {Thread.CurrentThread.ManagedThreadId}");
            return ((Task<string>)ar).Result;
        }
    }

    public class CalculatorClient
    {
        static int decimalPlaces = 12;
        public static void Main()
        {
            Calculator calc = new Calculator();
            int places = 35;

            AsyncCallback callBack = new AsyncCallback(PrintResult);
            IAsyncResult ar = calc.BeginCalculate(places, callBack, calc);

            // Do some work on this thread while the calculator is busy.
            Console.WriteLine("Working...");
            Thread.SpinWait(500000);
            Console.ReadLine();
        }

        public static void PrintResult(IAsyncResult result)
        {
            Calculator c = (Calculator)result.AsyncState;
            string piString = c.EndCalculate(result);
            Console.WriteLine($"Calling PrintResult on thread {Thread.CurrentThread.ManagedThreadId}; result = {piString}");
        }
    }
    #endregion
} //end APM_Task namespace

class FileStreamDemo
{
    static void Main()
    {
        FileStreamDemo fsd = new FileStreamDemo();
        //  var task = fsd.GetMultiFileData(new string[1]);
         fsd.ShowCallingFromAsync();
       // fsd.ShowCallingFromAsyncFileData();
        //  fsd.ProcessFilesAsync();
        //  task.Wait();
        //  Console.WriteLine(task.Result);

        Console.WriteLine("press any key");
        Console.ReadLine();
    }
    //<snippet03>
    const int MAX_FILE_SIZE = 14000000;
    public static Task<string> GetFileStringAsync(string path)
    {
        FileInfo fi = new FileInfo(path);
        byte[] data = null;
        data = new byte[fi.Length];

        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

        //Task<int> returns the number of bytes read
        Task<int> task = Task<int>.Factory.FromAsync(
                fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

        // It is possible to do other work here while waiting
        // for the antecedent task to complete.
        // ...

        // Add the continuation, which returns a Task<string>.
        return task.ContinueWith((antecedent) =>
        {
            fs.Close();

            // Result = "number of bytes read" (if we need it.)
            if (antecedent.Result < 100)
            {
                return "Data is too small to bother with.";
            }
            else
            {
                // If we did not receive the entire file, the end of the
                // data buffer will contain garbage.
                if (antecedent.Result < data.Length)
                    Array.Resize(ref data, antecedent.Result);

                // Will be returned in the Result property of the Task<string>
                // at some future point after the asynchronous file I/O operation completes.
                return new UTF8Encoding().GetString(data);
            }
        });
    }
    //</snippet03>

        void ShowCallingFromAsync()
        {
            string path = @"\\docbuild2\public\Main\Logs\DllDiffReport\DllDiffReport_BetweenInBuildAndInProduct.html";
            //<snippet04>

            Task<string> t = GetFileStringAsync(path);

            // Do some other work:
            // ...

            try
            {
                 Console.WriteLine(t.Result.Substring(0, 500));
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(ae.InnerException.Message);
            }
            //</snippet04>

            Console.ReadLine();
        }

        //not used???
        //<snippet12>
        public static Task<int> GetFileDataAsync(string path, ref byte[] data)
        {
            // Error handling omitted for brevity...

            FileInfo fi = new FileInfo(path);
            data = new byte[fi.Length];
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

            // Task<int> returns the number of bytes read
            // Pass FileStream in AsyncState parameter in order to close it from the call site.
            return Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, fs);
        }
        //</snippet12>

        void ShowCallingFromAsyncFileData()
        {
            //<snippet13>
            string path = @"\\remoteComputer\filename.txt";

            byte[] buffer = null;
            Task<int> t = GetFileDataAsync(path, ref buffer);

            // Can do some other work here:
            // ...
            for(int i = 0; i < 10; i++)
                Console.Write("Working... ");

            var result =
            t.ContinueWith((antecedent) =>
            {
                // Error handling omitted...

                return new UTF8Encoding().GetString(buffer);
            });

            // Can also do some other work here:
            // ...
            for (int i = 0; i < 10; i++)
                Console.Write("Working more... ");
            try
            {
                t.Wait();
                Console.WriteLine(result.Result.Substring(0, 50));
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(ae.InnerException.Message);
            }
            //</snippet13>

            Console.ReadLine();
        }
        // nested dummy class for snippet0 5 below
        class MyCustomState
        {
            public string StateData { get; set; }
            public MyCustomState() { StateData = "Hello"; }
        }
        private MyCustomState GetCustomState()
        {
            return new MyCustomState();
        }

        //<snippet05>
        public Task<string> GetFileStringAsync2(string path)
        {
            FileInfo fi = new FileInfo(path);
            byte[] data = new byte[fi.Length];
            MyCustomState state = GetCustomState();
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);
            // We still pass null for the last parameter because
            // the state variable is visible to the continuation delegate.
            Task<int> task = Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

            return task.ContinueWith((antecedent) =>
            {
                // It is safe to close the filestream now.
                fs.Close();

                // Capture custom state data directly in the user delegate.
                // No need to pass it through the FromAsync method.
                if (state.StateData.Contains("New York, New York"))
                {
                    return "Start spreading the news!";
                }
                else
                {
                    // If we did not receive the entire file, the end of the
                    // data buffer will contain garbage.
                    if (antecedent.Result < data.Length)
                        Array.Resize(ref data, antecedent.Result);

                    // Will be returned in the Result property of the Task<string>
                    // at some future point after the asynchronous file I/O operation completes.
                    return new UTF8Encoding().GetString(data);
                }
            });
        }
        //</snippet05>

        string[] GetFilesToRead()
        {
            return new string[3] { "test.txt", "test1.txt", "test2.txt" };
        }
        //<snippet06>
        public Task<string> GetMultiFileData(string[] filesToRead)
        {
            FileStream fs;
            Task<string>[] tasks = new Task<string>[filesToRead.Length];
            byte[] fileData = null;
            for (int i = 0; i < filesToRead.Length; i++)
            {
                fileData = new byte[0x1000];
                fs = new FileStream(filesToRead[i], FileMode.Open, FileAccess.Read, FileShare.Read, fileData.Length, true);

                // By adding the continuation here, the
                // Result of each task will be a string.
                tasks[i] = Task<int>.Factory.FromAsync(
                         fs.BeginRead, fs.EndRead, fileData, 0, fileData.Length, null)
                         .ContinueWith((antecedent) =>
                             {
                                 fs.Close();

                                 // If we did not receive the entire file, the end of the
                                 // data buffer will contain garbage.
                                 if (antecedent.Result < fileData.Length)
                                     Array.Resize(ref fileData, antecedent.Result);

                                 // Will be returned in the Result property of the Task<string>
                                 // at some future point after the asynchronous file I/O operation completes.
                                 return new UTF8Encoding().GetString(fileData);
                             });
            }

            // Wait for all tasks to complete.
            return Task<string>.Factory.ContinueWhenAll(tasks, (data) =>
            {
                // Propagate all exceptions and mark all faulted tasks as observed.
                Task.WaitAll(data);

                // Combine the results from all tasks.
                StringBuilder sb = new StringBuilder();
                foreach (var t in data)
                {
                    sb.Append(t.Result);
                }
                // Final result to be returned eventually on the calling thread.
                return sb.ToString();
            });
        }
        //</snippet06>
        public string ProcessData(Task[] arr)
        {
            return "processed";
        }

        //<snippet11>
        void ProcessFilesAsync()
        {
            string[] files = Directory.GetFiles(@"\\remoteComputer\folderName\", "*.*", SearchOption.AllDirectories);
            CancellationTokenSource cts = new CancellationTokenSource();

            List<Task<string>> tasks = new List<Task<string>>();

            foreach (var file in files)
            {
                Task<string> t = null;
                try
                {
                    t = GetFileStringAsync3(file, cts.Token);
                }
                catch
                {
                    Console.WriteLine($"Argument exception on {file}");
                    continue;
                }
                t.ContinueWith((antecedent) => ProcessFileData(antecedent.Result));
                tasks.Add(t);
            }
            Console.WriteLine("All tasks have been added.");
            // Do some work here.

            try
            {
                Task.WaitAll(tasks.ToArray());
                Console.WriteLine("All tasks have completed.");
            }
            catch (AggregateException ae)
            {
                foreach (var ex in ae.InnerExceptions)
                    Console.WriteLine(ex.Message);
            }
            finally
            {
                cts.Dispose();
            }

            // If needed, perform a final step after all tasks have completed.
            Task.Factory.ContinueWhenAll(tasks.ToArray(), (arr) =>
                {
                    foreach (var t in arr)
                    {
                        CompareFileData(t.Result);
                    }
                });
        }

        public Task<string> GetFileStringAsync3(string path, CancellationToken token)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(String.Format("Could not find {0}", path));
            }
            FileInfo fi = new FileInfo(path);
            byte[] data = null;

            if (fi.Length < MAX_FILE_SIZE && fi.Length > 0)
        {
            data = new byte[fi.Length];
        }
        else
            {
                throw new ArgumentException(String.Format("{0} is too big to load!", path));
            }

            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

            // We still pass null for the last parameter because
            // the state variable is visible to the continuation delegate.
            Task<int> task = Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

            return task.ContinueWith((antecedent) =>
            {
                fs.Close();

                // If we did not receive the entire file, the end of the
                // data buffer will contain garbage.
                if (antecedent.Result < data.Length)
                    Array.Resize(ref data, antecedent.Result);

                // Will be returned in the Result property of the Task<string>
                // at some future point after the asynchronous file I/O operation completes.
                return new UTF8Encoding().GetString(data);
            });
        }

        string ProcessFileData(string data)
        {

            string s = data.Substring(0, 80);
            Console.WriteLine(s);
            return s;
        }

        string CompareFileData(string s)
        {
            return "compare results";
        }
        //</snippet11>
    }

sample · vbsamples/snippets/visualbasic/VS_Snippets_Misc/fromasync/vb/module1.vb01

vb
Imports System.Collections.Generic
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.Threading.Tasks

Namespace specialNamespaceForOneMethodSignature
    Class Task(Of TResult)
        Public TValue As TResult
    End Class
    Class Dummy(Of TResult)
        '<snippet01>
        Public Function FromAsync(Of TArg1, TArg2, TArg3)(
                        ByVal beginMethod As Func(Of TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult),
                        ByVal endMethod As Func(Of IAsyncResult, TResult),
                        ByVal dataBuffer As TArg1,
                        ByVal byteOffsetToStartAt As TArg2,
                        ByVal maxBytesToRead As TArg3,
                        ByVal stateInfo As Object)
            '</snippet01>
            Return New Task(Of TResult)
        End Function


    End Class
End Namespace

Module Module1

    Class AsyncResult
        Public Sub New()

        End Sub
        '<snippet07>
        Shared Function ReturnTaskFromAsyncResult() As Task(Of String)
            Dim ar As IAsyncResult = DoSomethingAsynchronously()
            Dim t As Task(Of String) = Task(Of String).Factory.FromAsync(ar, Function(res) CStr(res.AsyncState))
            Return t
        End Function
        '</snippet07>

        Private Shared Function DoSomethingAsynchronously() As IAsyncResult
            Throw New NotImplementedException
        End Function

    End Class
    '<snippet08>
    Class WebDataDownLoader

        Dim tcs As New TaskCompletionSource(Of String())
        Dim nameToSearch As String
        Dim token As CancellationToken
        Dim results As New List(Of String)
        Dim m_lock As Object
        Dim count As Integer
        Dim addresses() As String

        Shared Sub Main()

            Dim downloader As New WebDataDownLoader()
            downloader.addresses = {"http://www.msnbc.com", "http://www.yahoo.com", _
                                         "http://www.nytimes.com", "http://www.washingtonpost.com", _
                                         "http://www.latimes.com", "http://www.newsday.com"}
            Dim cts As New CancellationTokenSource()

            ' Create a UI thread from which to cancel the entire operation
            Task.Factory.StartNew(Sub()
                                      Console.WriteLine("Press c to cancel")
                                      If Console.ReadKey().KeyChar = "c"c Then
                                          cts.Cancel()
                                      End If
                                  End Sub)

            ' Using a neutral search term that is sure to get some hits on English web sites.
            ' Please substitute your favorite search term.
            downloader.nameToSearch = "the"
            Dim webTask As Task(Of String()) = downloader.GetWordCounts(downloader.addresses, downloader.nameToSearch, cts.Token)

            ' Do some other work here unless the method has already completed.
            If (webTask.IsCompleted = False) Then
                ' Simulate some work
                Thread.SpinWait(5000000)
            End If

            Dim results As String() = Nothing
            Try
                results = webTask.Result
            Catch ae As AggregateException
                For Each ex As Exception In ae.InnerExceptions
                    If (TypeOf (ex) Is OperationCanceledException) Then
                        Dim oce As OperationCanceledException = CType(ex, OperationCanceledException)
                        If oce.CancellationToken = cts.Token Then
                            Console.WriteLine("Operation canceled by user.")
                        End If
                    Else
                        Console.WriteLine(ex.Message)
                    End If

                Next
            Finally
                cts.Dispose()
            End Try

            If (Not results Is Nothing) Then
                For Each item As String In results
                    Console.WriteLine(item)
                Next
            End If

            Console.WriteLine("Press any key to exit")
            Console.ReadKey()
        End Sub

        Public Function GetWordCounts(ByVal urls() As String, ByVal str As String, ByVal token As CancellationToken) As Task(Of String())

            Dim webClients() As WebClient
            ReDim webClients(urls.Length)
            m_lock = New Object()

            ' If the user cancels the CancellationToken, then we can use the
            ' WebClient's ability to cancel its own async operations.
            token.Register(Sub()
                               For Each wc As WebClient In webClients
                                   If Not wc Is Nothing Then
                                       wc.CancelAsync()
                                   End If
                               Next
                           End Sub)


            For i As Integer = 0 To urls.Length - 1
                webClients(i) = New WebClient()

                ' Specify the callback for the DownloadStringCompleted
                ' event that will be raised by this WebClient instance.
                AddHandler webClients(i).DownloadStringCompleted, AddressOf WebEventHandler

                Dim address As Uri = Nothing
                Try
                    address = New Uri(urls(i))
                    ' Pass the address, and also use it for the userToken 
                    ' to identify the page when the delegate is invoked.
                    webClients(i).DownloadStringAsync(address, address)
                Catch ex As UriFormatException
                    tcs.TrySetException(ex)
                    Return tcs.Task
                End Try

            Next

            ' Return the underlying Task. The client code
            ' waits on the Result property, and handles exceptions
            ' in the try-catch block there.
            Return tcs.Task
        End Function



        Public Sub WebEventHandler(ByVal sender As Object, ByVal args As DownloadStringCompletedEventArgs)

            If args.Cancelled = True Then
                tcs.TrySetCanceled()
                Return
            ElseIf Not args.Error Is Nothing Then
                tcs.TrySetException(args.Error)
                Return
            Else
                ' Split the string into an array of words,
                ' then count the number of elements that match
                ' the search term.
                Dim words() As String = args.Result.Split(" "c)
                Dim NAME As String = nameToSearch.ToUpper()
                Dim nameCount = (From word In words.AsParallel()
                                 Where word.ToUpper().Contains(NAME)
                                 Select word).Count()

                ' Associate the results with the url, and add new string to the array that 
                ' the underlying Task object will return in its Result property.
                results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, nameToSearch))
            End If

            SyncLock (m_lock)
                count = count + 1
                If (count = addresses.Length) Then
                    tcs.TrySetResult(results.ToArray())
                End If
            End SyncLock
        End Sub
        '</snippet08>
    End Class


    '<snippet09>
    Class Calculator
        Public Function BeginCalculate(ByVal decimalPlaces As Integer, ByVal ac As AsyncCallback, ByVal state As Object) As IAsyncResult
            Console.WriteLine("Calling BeginCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId)
            Dim myTask = Task(Of String).Factory.StartNew(Function(obj) Compute(decimalPlaces), state)
            myTask.ContinueWith(Sub(antecedent) ac(myTask))

        End Function
        Private Function Compute(ByVal decimalPlaces As Integer)
            Console.WriteLine("Calling compute on thread {0}", Thread.CurrentThread.ManagedThreadId)

            ' Simulating some heavy work.
            Thread.SpinWait(500000000)

            ' Actual implementation left as exercise for the reader.
            ' Several examples are available on the Web.
            Return "3.14159265358979323846264338327950288"
        End Function

        Public Function EndCalculate(ByVal ar As IAsyncResult) As String
            Console.WriteLine("Calling EndCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId)
            Return CType(ar, Task(Of String)).Result
        End Function
    End Class

    Class CalculatorClient
        Shared decimalPlaces As Integer
        Shared Sub Main()
            Dim calc As New Calculator
            Dim places As Integer = 35
            Dim callback As New AsyncCallback(AddressOf PrintResult)
            Dim ar As IAsyncResult = calc.BeginCalculate(places, callback, calc)

            ' Do some work on this thread while the calculator is busy.
            Console.WriteLine("Working...")
            Thread.SpinWait(500000)
            Console.ReadLine()
        End Sub

        Public Shared Sub PrintResult(ByVal result As IAsyncResult)
            Dim c As Calculator = CType(result.AsyncState, Calculator)
            Dim piString As String = c.EndCalculate(result)
            Console.WriteLine("Calling PrintResult on thread {0}; result = {1}",
                       Thread.CurrentThread.ManagedThreadId, piString)
        End Sub

    End Class
    '</snippet09>

    Class FileStreamDemo
        Shared Sub Main()
            Dim fsd As New FileStreamDemo()
            fsd.ShowCallingFromAsync()
        End Sub

        '<snippet03>
        Const MAX_FILE_SIZE As Integer = 14000000
        Shared Function GetFileStringAsync(ByVal path As String) As Task(Of String)
            Dim fi As New FileInfo(path)
            Dim data(fi.Length - 1) As Byte

            Dim fs As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, True)

            ' Task(Of Integer) returns the number of bytes read
            Dim myTask As Task(Of Integer) = Task(Of Integer).Factory.FromAsync(
                AddressOf fs.BeginRead, AddressOf fs.EndRead, data, 0, data.Length, Nothing)

            ' It is possible to do other work here while waiting
            ' for the antecedent task to complete.
            ' ...

            ' Add the continuation, which returns a Task<string>. 
            Return myTask.ContinueWith(Function(antecedent)
                                           fs.Close()
                                           If (antecedent.Result < 100) Then
                                               Return "Data is too small to bother with."
                                           End If
                                           ' If we did not receive the entire file, the end of the
                                           ' data buffer will contain garbage.
                                           If (antecedent.Result < data.Length) Then
                                               Array.Resize(data, antecedent.Result)
                                           End If

                                           ' Will be returned in the Result property of the Task<string>
                                           ' at some future point after the asynchronous file I/O operation completes.
                                           Return New UTF8Encoding().GetString(data)
                                       End Function)

        End Function
        '</snippet03>

        Public Sub ShowCallingFromAsync()
            Dim path As String = "\\docbuild2\public\Main\Logs\DllDiffReport\DllDiffReport_BetweenInBuildAndInPublicShare.html"
            '<snippet04>
            Dim myTask As Task(Of String) = GetFileStringAsync(path)

            ' Do some other work
            ' ...

            Try
                Console.WriteLine(myTask.Result.Substring(0, 500))
            Catch ex As AggregateException
                Console.WriteLine(ex.InnerException.Message)
            End Try
            '</snippet04>

            Console.WriteLine("Press Enter to exit.")
            Console.ReadLine()
        End Sub

        '<snippet05>
        Public Function GetFileStringAsync2(ByVal path As String) As Task(Of String)
            Dim fi = New FileInfo(path)
            Dim data(fi.Length - 1) As Byte
            Dim state As New MyCustomState()

            Dim fs As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, True)
            ' We still pass null for the last parameter because
            ' the state variable is visible to the continuation delegate.
            Dim myTask As Task(Of Integer) = Task(Of Integer).Factory.FromAsync(
                    AddressOf fs.BeginRead, AddressOf fs.EndRead, data, 0, data.Length, Nothing)

            Return myTask.ContinueWith(Function(antecedent)
                                           fs.Close()
                                           ' Capture custom state data directly in the user delegate.
                                           ' No need to pass it through the FromAsync method.
                                           If (state.StateData.Contains("New York, New York")) Then
                                               Return "Start spreading the news!"
                                           End If

                                           ' If we did not receive the entire file, the end of the
                                           ' data buffer will contain garbage.
                                           If (antecedent.Result < data.Length) Then
                                               Array.Resize(data, antecedent.Result)
                                           End If
                                           '/ Will be returned in the Result property of the Task<string>
                                           '/ at some future point after the asynchronous file I/O operation completes.
                                           Return New UTF8Encoding().GetString(data)
                                       End Function)

        End Function
        '</snippet05>


        '<snippet06>
        Public Function GetMultiFileData(ByVal filesToRead As String()) As Task(Of String)
            Dim fs As FileStream
            Dim tasks(filesToRead.Length - 1) As Task(Of String)
            Dim fileData() As Byte = Nothing
            For i As Integer = 0 To filesToRead.Length
                fileData(&H1000) = New Byte()
                fs = New FileStream(filesToRead(i), FileMode.Open, FileAccess.Read, FileShare.Read, fileData.Length, True)

                ' By adding the continuation here, the 
                ' Result of each task will be a string.
                tasks(i) = Task(Of Integer).Factory.FromAsync(AddressOf fs.BeginRead,
                                                              AddressOf fs.EndRead,
                                                              fileData,
                                                              0,
                                                              fileData.Length,
                                                              Nothing).
                                                          ContinueWith(Function(antecedent)
                                                                           fs.Close()
                                                                           'If we did not receive the entire file, the end of the
                                                                           ' data buffer will contain garbage.
                                                                           If (antecedent.Result < fileData.Length) Then
                                                                               ReDim Preserve fileData(antecedent.Result)
                                                                           End If

                                                                           'Will be returned in the Result property of the Task<string>
                                                                           ' at some future point after the asynchronous file I/O operation completes.
                                                                           Return New UTF8Encoding().GetString(fileData)
                                                                       End Function)
            Next

            Return Task(Of String).Factory.ContinueWhenAll(tasks, Function(data)

                                                                      ' Propagate all exceptions and mark all faulted tasks as observed.
                                                                      Task.WaitAll(data)

                                                                      ' Combine the results from all tasks.
                                                                      Dim sb As New StringBuilder()
                                                                      For Each t As Task(Of String) In data
                                                                          sb.Append(t.Result)
                                                                      Next
                                                                      ' Final result to be returned eventually on the calling thread.
                                                                      Return sb.ToString()
                                                                  End Function)
        End Function
        '</snippet06>

        Class MyCustomState
            Public StateData As String
            Public Sub New()
                StateData = "Hello"
            End Sub
        End Class


    End Class


End Module

This overload takes three input parameters, as follows. The first parameter is a System.Func delegate that matches the signature of the System.IO.FileStream.BeginRead method. The second parameter is a System.Func delegate that takes an System.IAsyncResult and returns a TResult. Because System.IO.FileStream.EndRead returns an integer, the compiler infers the type of TResult as System.Int32 and the type of the task as System.Threading.Tasks.Task. The last four parameters are identical to those in the System.IO.FileStream.BeginRead method:

  • The buffer in which to store the file data.

  • The offset in the buffer at which to begin writing data.

  • The maximum amount of data to read from the file.

  • An optional object that stores user-defined state data to pass to the callback.

Use ContinueWith for the callback functionality

If you require access to the data in the file, as opposed to just the number of bytes, the System.Threading.Tasks.TaskFactory.FromAsync method is not sufficient. Instead, use System.Threading.Tasks.Task, whose Result property contains the file data. You can do this by adding a continuation to the original task. The continuation performs the work that would typically be performed by the System.AsyncCallback delegate. It is invoked when the antecedent completes, and the data buffer has been filled. (The System.IO.FileStream object should be closed before returning.)

The following example shows how to return a System.Threading.Tasks.Task that encapsulates the BeginRead/EndRead pair of the System.IO.FileStream class.

sample · csharpsamples/snippets/csharp/VS_Snippets_Misc/fromasync/cs/fromasync.cs03

csharp
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net;

namespace specialNamespaceForOneMethodSignature
{
    using System;
    class Task<TResult>
    {
        public TResult TValue;
    }
    class Dummy <TResult>
    {
            // <snippet01>
            public Task<TResult> FromAsync<TArg1, TArg2, TArg3>(
                Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, //BeginRead
                 Func<IAsyncResult, TResult> endMethod, //EndRead
                 TArg1 arg1, // the byte[] buffer
                 TArg2 arg2, // the offset in arg1 at which to start writing data
                 TArg3 arg3, // the maximum number of bytes to read
                 object state // optional state information
                )
                //</snippet01>
        { return new Task<TResult>(); }
    }
}

namespace APM_Task
{
    class TestFileStreamNaive
    {
        //<snippet02>
        public string GetFileData(string path)
        {
            FileInfo fi = new FileInfo(path);
            byte[] data = new byte[fi.Length];
            FileStream fs;

            // a using statement is ok here because we Wait before returning
            // and will be done with the filestream.
            using (fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true))
            {
                //Task<int> returns the number of bytes read
                Task<int> task = Task<int>.Factory.FromAsync(
                        fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

                task.Wait();
                return new UTF8Encoding().GetString(data);
            }
        }
        //</snippet02>
        static void Main()
        {
            TestFileStreamNaive instance = new TestFileStreamNaive();
            string s = instance.GetFileData("testfile.txt");
        }
    }

    class AsyncResult : IAsyncResult
    {

        public AsyncResult() { }
        public object AsyncState
        {
            get { return "Hello from a parallel universe."; }
        }

        public WaitHandle AsyncWaitHandle
        {
            get { throw new NotImplementedException(); }
        }

        public bool CompletedSynchronously
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsCompleted
        {
            get { throw new NotImplementedException(); }
        }

        static IAsyncResult DoSomethingAsynchronously()
        {
            return new AsyncResult();
        }

        //<snippet07>
        static Task<String> ReturnTaskFromAsyncResult()
        {
            IAsyncResult ar = DoSomethingAsynchronously();
            Task<String> t = Task<string>.Factory.FromAsync(ar, _ =>
                {
                    return (string)ar.AsyncState;
                });

            return t;
        }
        //</snippet07>
    }

    //<snippet08>
    class WebDataDownloader
    {

        static void Main()
        {
            WebDataDownloader downloader = new WebDataDownloader();
            string[] addresses = { "http://www.msnbc.com", "http://www.yahoo.com",
                                     "http://www.nytimes.com", "http://www.washingtonpost.com",
                                     "http://www.latimes.com", "http://www.newsday.com" };
            CancellationTokenSource cts = new CancellationTokenSource();

            // Create a UI thread from which to cancel the entire operation
            Task.Factory.StartNew(() =>
            {
                Console.WriteLine("Press c to cancel");
                if (Console.ReadKey().KeyChar == 'c')
                    cts.Cancel();
            });

            // Using a neutral search term that is sure to get some hits.
            Task<string[]> webTask = downloader.GetWordCounts(addresses, "the", cts.Token);

            // Do some other work here unless the method has already completed.
            if (!webTask.IsCompleted)
            {
                // Simulate some work.
                Thread.SpinWait(5000000);
            }

            string[] results = null;
            try
            {
                results = webTask.Result;
            }
            catch (AggregateException e)
            {
                foreach (var ex in e.InnerExceptions)
                {
                    OperationCanceledException oce = ex as OperationCanceledException;
                    if (oce != null)
                    {
                        if (oce.CancellationToken == cts.Token)
                        {
                            Console.WriteLine("Operation canceled by user.");
                        }
                    }
                    else
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            finally
            {
                cts.Dispose();
            }
            if (results != null)
            {
                foreach (var item in results)
                    Console.WriteLine(item);
            }
            Console.ReadKey();
        }

        Task<string[]> GetWordCounts(string[] urls, string name, CancellationToken token)
        {
            TaskCompletionSource<string[]> tcs = new TaskCompletionSource<string[]>();
            WebClient[] webClients = new WebClient[urls.Length];

            // If the user cancels the CancellationToken, then we can use the
            // WebClient's ability to cancel its own async operations.
            token.Register(() =>
                {
                    foreach (var wc in webClients)
                    {
                        if (wc != null)
                            wc.CancelAsync();
                    }
                });

            object m_lock = new object();
            int count = 0;
            List<string> results = new List<string>();
            for (int i = 0; i < urls.Length; i++)
            {
                webClients[i] = new WebClient();

                #region callback
                // Specify the callback for the DownloadStringCompleted
                // event that will be raised by this WebClient instance.
                webClients[i].DownloadStringCompleted += (obj, args) =>
                 {
                     if (args.Cancelled == true)
                     {
                         tcs.TrySetCanceled();
                         return;
                     }
                     else if (args.Error != null)
                     {
                         // Pass through to the underlying Task
                         // any exceptions thrown by the WebClient
                         // during the asynchronous operation.
                         tcs.TrySetException(args.Error);
                         return;
                     }
                     else
                     {
                         // Split the string into an array of words,
                         // then count the number of elements that match
                         // the search term.
                         string[] words = null;
                         words = args.Result.Split(' ');
                         string NAME = name.ToUpper();
                         int nameCount = (from word in words.AsParallel()
                                          where word.ToUpper().Contains(NAME)
                                          select word)
                                         .Count();

                         // Associate the results with the url, and add new string to the array that
                         // the underlying Task object will return in its Result property.
                         results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, name));
                     }

                     // If this is the last async operation to complete,
                     // then set the Result property on the underlying Task.
                     lock (m_lock)
                     {
                         count++;
                         if (count == urls.Length)
                         {
                             tcs.TrySetResult(results.ToArray());
                         }
                     }
                 };
                #endregion

                // Call DownloadStringAsync for each URL.
                Uri address = null;
                try
                {
                    address = new Uri(urls[i]);
                    // Pass the address, and also use it for the userToken
                    // to identify the page when the delegate is invoked.
                    webClients[i].DownloadStringAsync(address, address);
                }

                catch (UriFormatException ex)
                {
                    // Abandon the entire operation if one url is malformed.
                    // Other actions are possible here.
                    tcs.TrySetException(ex);
                    return tcs.Task;
                }
            }

            // Return the underlying Task. The client code
            // waits on the Result property, and handles exceptions
            // in the try-catch block there.
            return tcs.Task;
        }
        //</snippet08>
    }

    #region snippet09
    class Calculator
    {
        public IAsyncResult BeginCalculate(int decimalPlaces, AsyncCallback ac, object state)
        {
            Console.WriteLine($"Calling BeginCalculate on thread {Thread.CurrentThread.ManagedThreadId}");
            Task<string> f = Task<string>.Factory.StartNew(_ => Compute(decimalPlaces), state);
            if (ac != null) f.ContinueWith((res) => ac(f));
            return f;
        }

        public string Compute(int numPlaces)
        {
            Console.WriteLine($"Calling compute on thread {Thread.CurrentThread.ManagedThreadId}");

            // Simulating some heavy work.
            Thread.SpinWait(500000000);

            // Actual implementation left as exercise for the reader.
            // Several examples are available on the Web.
            return "3.14159265358979323846264338327950288";
        }

        public string EndCalculate(IAsyncResult ar)
        {
            Console.WriteLine($"Calling EndCalculate on thread {Thread.CurrentThread.ManagedThreadId}");
            return ((Task<string>)ar).Result;
        }
    }

    public class CalculatorClient
    {
        static int decimalPlaces = 12;
        public static void Main()
        {
            Calculator calc = new Calculator();
            int places = 35;

            AsyncCallback callBack = new AsyncCallback(PrintResult);
            IAsyncResult ar = calc.BeginCalculate(places, callBack, calc);

            // Do some work on this thread while the calculator is busy.
            Console.WriteLine("Working...");
            Thread.SpinWait(500000);
            Console.ReadLine();
        }

        public static void PrintResult(IAsyncResult result)
        {
            Calculator c = (Calculator)result.AsyncState;
            string piString = c.EndCalculate(result);
            Console.WriteLine($"Calling PrintResult on thread {Thread.CurrentThread.ManagedThreadId}; result = {piString}");
        }
    }
    #endregion
} //end APM_Task namespace

class FileStreamDemo
{
    static void Main()
    {
        FileStreamDemo fsd = new FileStreamDemo();
        //  var task = fsd.GetMultiFileData(new string[1]);
         fsd.ShowCallingFromAsync();
       // fsd.ShowCallingFromAsyncFileData();
        //  fsd.ProcessFilesAsync();
        //  task.Wait();
        //  Console.WriteLine(task.Result);

        Console.WriteLine("press any key");
        Console.ReadLine();
    }
    //<snippet03>
    const int MAX_FILE_SIZE = 14000000;
    public static Task<string> GetFileStringAsync(string path)
    {
        FileInfo fi = new FileInfo(path);
        byte[] data = null;
        data = new byte[fi.Length];

        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

        //Task<int> returns the number of bytes read
        Task<int> task = Task<int>.Factory.FromAsync(
                fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

        // It is possible to do other work here while waiting
        // for the antecedent task to complete.
        // ...

        // Add the continuation, which returns a Task<string>.
        return task.ContinueWith((antecedent) =>
        {
            fs.Close();

            // Result = "number of bytes read" (if we need it.)
            if (antecedent.Result < 100)
            {
                return "Data is too small to bother with.";
            }
            else
            {
                // If we did not receive the entire file, the end of the
                // data buffer will contain garbage.
                if (antecedent.Result < data.Length)
                    Array.Resize(ref data, antecedent.Result);

                // Will be returned in the Result property of the Task<string>
                // at some future point after the asynchronous file I/O operation completes.
                return new UTF8Encoding().GetString(data);
            }
        });
    }
    //</snippet03>

        void ShowCallingFromAsync()
        {
            string path = @"\\docbuild2\public\Main\Logs\DllDiffReport\DllDiffReport_BetweenInBuildAndInProduct.html";
            //<snippet04>

            Task<string> t = GetFileStringAsync(path);

            // Do some other work:
            // ...

            try
            {
                 Console.WriteLine(t.Result.Substring(0, 500));
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(ae.InnerException.Message);
            }
            //</snippet04>

            Console.ReadLine();
        }

        //not used???
        //<snippet12>
        public static Task<int> GetFileDataAsync(string path, ref byte[] data)
        {
            // Error handling omitted for brevity...

            FileInfo fi = new FileInfo(path);
            data = new byte[fi.Length];
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

            // Task<int> returns the number of bytes read
            // Pass FileStream in AsyncState parameter in order to close it from the call site.
            return Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, fs);
        }
        //</snippet12>

        void ShowCallingFromAsyncFileData()
        {
            //<snippet13>
            string path = @"\\remoteComputer\filename.txt";

            byte[] buffer = null;
            Task<int> t = GetFileDataAsync(path, ref buffer);

            // Can do some other work here:
            // ...
            for(int i = 0; i < 10; i++)
                Console.Write("Working... ");

            var result =
            t.ContinueWith((antecedent) =>
            {
                // Error handling omitted...

                return new UTF8Encoding().GetString(buffer);
            });

            // Can also do some other work here:
            // ...
            for (int i = 0; i < 10; i++)
                Console.Write("Working more... ");
            try
            {
                t.Wait();
                Console.WriteLine(result.Result.Substring(0, 50));
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(ae.InnerException.Message);
            }
            //</snippet13>

            Console.ReadLine();
        }
        // nested dummy class for snippet0 5 below
        class MyCustomState
        {
            public string StateData { get; set; }
            public MyCustomState() { StateData = "Hello"; }
        }
        private MyCustomState GetCustomState()
        {
            return new MyCustomState();
        }

        //<snippet05>
        public Task<string> GetFileStringAsync2(string path)
        {
            FileInfo fi = new FileInfo(path);
            byte[] data = new byte[fi.Length];
            MyCustomState state = GetCustomState();
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);
            // We still pass null for the last parameter because
            // the state variable is visible to the continuation delegate.
            Task<int> task = Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

            return task.ContinueWith((antecedent) =>
            {
                // It is safe to close the filestream now.
                fs.Close();

                // Capture custom state data directly in the user delegate.
                // No need to pass it through the FromAsync method.
                if (state.StateData.Contains("New York, New York"))
                {
                    return "Start spreading the news!";
                }
                else
                {
                    // If we did not receive the entire file, the end of the
                    // data buffer will contain garbage.
                    if (antecedent.Result < data.Length)
                        Array.Resize(ref data, antecedent.Result);

                    // Will be returned in the Result property of the Task<string>
                    // at some future point after the asynchronous file I/O operation completes.
                    return new UTF8Encoding().GetString(data);
                }
            });
        }
        //</snippet05>

        string[] GetFilesToRead()
        {
            return new string[3] { "test.txt", "test1.txt", "test2.txt" };
        }
        //<snippet06>
        public Task<string> GetMultiFileData(string[] filesToRead)
        {
            FileStream fs;
            Task<string>[] tasks = new Task<string>[filesToRead.Length];
            byte[] fileData = null;
            for (int i = 0; i < filesToRead.Length; i++)
            {
                fileData = new byte[0x1000];
                fs = new FileStream(filesToRead[i], FileMode.Open, FileAccess.Read, FileShare.Read, fileData.Length, true);

                // By adding the continuation here, the
                // Result of each task will be a string.
                tasks[i] = Task<int>.Factory.FromAsync(
                         fs.BeginRead, fs.EndRead, fileData, 0, fileData.Length, null)
                         .ContinueWith((antecedent) =>
                             {
                                 fs.Close();

                                 // If we did not receive the entire file, the end of the
                                 // data buffer will contain garbage.
                                 if (antecedent.Result < fileData.Length)
                                     Array.Resize(ref fileData, antecedent.Result);

                                 // Will be returned in the Result property of the Task<string>
                                 // at some future point after the asynchronous file I/O operation completes.
                                 return new UTF8Encoding().GetString(fileData);
                             });
            }

            // Wait for all tasks to complete.
            return Task<string>.Factory.ContinueWhenAll(tasks, (data) =>
            {
                // Propagate all exceptions and mark all faulted tasks as observed.
                Task.WaitAll(data);

                // Combine the results from all tasks.
                StringBuilder sb = new StringBuilder();
                foreach (var t in data)
                {
                    sb.Append(t.Result);
                }
                // Final result to be returned eventually on the calling thread.
                return sb.ToString();
            });
        }
        //</snippet06>
        public string ProcessData(Task[] arr)
        {
            return "processed";
        }

        //<snippet11>
        void ProcessFilesAsync()
        {
            string[] files = Directory.GetFiles(@"\\remoteComputer\folderName\", "*.*", SearchOption.AllDirectories);
            CancellationTokenSource cts = new CancellationTokenSource();

            List<Task<string>> tasks = new List<Task<string>>();

            foreach (var file in files)
            {
                Task<string> t = null;
                try
                {
                    t = GetFileStringAsync3(file, cts.Token);
                }
                catch
                {
                    Console.WriteLine($"Argument exception on {file}");
                    continue;
                }
                t.ContinueWith((antecedent) => ProcessFileData(antecedent.Result));
                tasks.Add(t);
            }
            Console.WriteLine("All tasks have been added.");
            // Do some work here.

            try
            {
                Task.WaitAll(tasks.ToArray());
                Console.WriteLine("All tasks have completed.");
            }
            catch (AggregateException ae)
            {
                foreach (var ex in ae.InnerExceptions)
                    Console.WriteLine(ex.Message);
            }
            finally
            {
                cts.Dispose();
            }

            // If needed, perform a final step after all tasks have completed.
            Task.Factory.ContinueWhenAll(tasks.ToArray(), (arr) =>
                {
                    foreach (var t in arr)
                    {
                        CompareFileData(t.Result);
                    }
                });
        }

        public Task<string> GetFileStringAsync3(string path, CancellationToken token)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(String.Format("Could not find {0}", path));
            }
            FileInfo fi = new FileInfo(path);
            byte[] data = null;

            if (fi.Length < MAX_FILE_SIZE && fi.Length > 0)
        {
            data = new byte[fi.Length];
        }
        else
            {
                throw new ArgumentException(String.Format("{0} is too big to load!", path));
            }

            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

            // We still pass null for the last parameter because
            // the state variable is visible to the continuation delegate.
            Task<int> task = Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

            return task.ContinueWith((antecedent) =>
            {
                fs.Close();

                // If we did not receive the entire file, the end of the
                // data buffer will contain garbage.
                if (antecedent.Result < data.Length)
                    Array.Resize(ref data, antecedent.Result);

                // Will be returned in the Result property of the Task<string>
                // at some future point after the asynchronous file I/O operation completes.
                return new UTF8Encoding().GetString(data);
            });
        }

        string ProcessFileData(string data)
        {

            string s = data.Substring(0, 80);
            Console.WriteLine(s);
            return s;
        }

        string CompareFileData(string s)
        {
            return "compare results";
        }
        //</snippet11>
    }

sample · vbsamples/snippets/visualbasic/VS_Snippets_Misc/fromasync/vb/module1.vb03

vb
Imports System.Collections.Generic
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.Threading.Tasks

Namespace specialNamespaceForOneMethodSignature
    Class Task(Of TResult)
        Public TValue As TResult
    End Class
    Class Dummy(Of TResult)
        '<snippet01>
        Public Function FromAsync(Of TArg1, TArg2, TArg3)(
                        ByVal beginMethod As Func(Of TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult),
                        ByVal endMethod As Func(Of IAsyncResult, TResult),
                        ByVal dataBuffer As TArg1,
                        ByVal byteOffsetToStartAt As TArg2,
                        ByVal maxBytesToRead As TArg3,
                        ByVal stateInfo As Object)
            '</snippet01>
            Return New Task(Of TResult)
        End Function


    End Class
End Namespace

Module Module1

    Class AsyncResult
        Public Sub New()

        End Sub
        '<snippet07>
        Shared Function ReturnTaskFromAsyncResult() As Task(Of String)
            Dim ar As IAsyncResult = DoSomethingAsynchronously()
            Dim t As Task(Of String) = Task(Of String).Factory.FromAsync(ar, Function(res) CStr(res.AsyncState))
            Return t
        End Function
        '</snippet07>

        Private Shared Function DoSomethingAsynchronously() As IAsyncResult
            Throw New NotImplementedException
        End Function

    End Class
    '<snippet08>
    Class WebDataDownLoader

        Dim tcs As New TaskCompletionSource(Of String())
        Dim nameToSearch As String
        Dim token As CancellationToken
        Dim results As New List(Of String)
        Dim m_lock As Object
        Dim count As Integer
        Dim addresses() As String

        Shared Sub Main()

            Dim downloader As New WebDataDownLoader()
            downloader.addresses = {"http://www.msnbc.com", "http://www.yahoo.com", _
                                         "http://www.nytimes.com", "http://www.washingtonpost.com", _
                                         "http://www.latimes.com", "http://www.newsday.com"}
            Dim cts As New CancellationTokenSource()

            ' Create a UI thread from which to cancel the entire operation
            Task.Factory.StartNew(Sub()
                                      Console.WriteLine("Press c to cancel")
                                      If Console.ReadKey().KeyChar = "c"c Then
                                          cts.Cancel()
                                      End If
                                  End Sub)

            ' Using a neutral search term that is sure to get some hits on English web sites.
            ' Please substitute your favorite search term.
            downloader.nameToSearch = "the"
            Dim webTask As Task(Of String()) = downloader.GetWordCounts(downloader.addresses, downloader.nameToSearch, cts.Token)

            ' Do some other work here unless the method has already completed.
            If (webTask.IsCompleted = False) Then
                ' Simulate some work
                Thread.SpinWait(5000000)
            End If

            Dim results As String() = Nothing
            Try
                results = webTask.Result
            Catch ae As AggregateException
                For Each ex As Exception In ae.InnerExceptions
                    If (TypeOf (ex) Is OperationCanceledException) Then
                        Dim oce As OperationCanceledException = CType(ex, OperationCanceledException)
                        If oce.CancellationToken = cts.Token Then
                            Console.WriteLine("Operation canceled by user.")
                        End If
                    Else
                        Console.WriteLine(ex.Message)
                    End If

                Next
            Finally
                cts.Dispose()
            End Try

            If (Not results Is Nothing) Then
                For Each item As String In results
                    Console.WriteLine(item)
                Next
            End If

            Console.WriteLine("Press any key to exit")
            Console.ReadKey()
        End Sub

        Public Function GetWordCounts(ByVal urls() As String, ByVal str As String, ByVal token As CancellationToken) As Task(Of String())

            Dim webClients() As WebClient
            ReDim webClients(urls.Length)
            m_lock = New Object()

            ' If the user cancels the CancellationToken, then we can use the
            ' WebClient's ability to cancel its own async operations.
            token.Register(Sub()
                               For Each wc As WebClient In webClients
                                   If Not wc Is Nothing Then
                                       wc.CancelAsync()
                                   End If
                               Next
                           End Sub)


            For i As Integer = 0 To urls.Length - 1
                webClients(i) = New WebClient()

                ' Specify the callback for the DownloadStringCompleted
                ' event that will be raised by this WebClient instance.
                AddHandler webClients(i).DownloadStringCompleted, AddressOf WebEventHandler

                Dim address As Uri = Nothing
                Try
                    address = New Uri(urls(i))
                    ' Pass the address, and also use it for the userToken 
                    ' to identify the page when the delegate is invoked.
                    webClients(i).DownloadStringAsync(address, address)
                Catch ex As UriFormatException
                    tcs.TrySetException(ex)
                    Return tcs.Task
                End Try

            Next

            ' Return the underlying Task. The client code
            ' waits on the Result property, and handles exceptions
            ' in the try-catch block there.
            Return tcs.Task
        End Function



        Public Sub WebEventHandler(ByVal sender As Object, ByVal args As DownloadStringCompletedEventArgs)

            If args.Cancelled = True Then
                tcs.TrySetCanceled()
                Return
            ElseIf Not args.Error Is Nothing Then
                tcs.TrySetException(args.Error)
                Return
            Else
                ' Split the string into an array of words,
                ' then count the number of elements that match
                ' the search term.
                Dim words() As String = args.Result.Split(" "c)
                Dim NAME As String = nameToSearch.ToUpper()
                Dim nameCount = (From word In words.AsParallel()
                                 Where word.ToUpper().Contains(NAME)
                                 Select word).Count()

                ' Associate the results with the url, and add new string to the array that 
                ' the underlying Task object will return in its Result property.
                results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, nameToSearch))
            End If

            SyncLock (m_lock)
                count = count + 1
                If (count = addresses.Length) Then
                    tcs.TrySetResult(results.ToArray())
                End If
            End SyncLock
        End Sub
        '</snippet08>
    End Class


    '<snippet09>
    Class Calculator
        Public Function BeginCalculate(ByVal decimalPlaces As Integer, ByVal ac As AsyncCallback, ByVal state As Object) As IAsyncResult
            Console.WriteLine("Calling BeginCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId)
            Dim myTask = Task(Of String).Factory.StartNew(Function(obj) Compute(decimalPlaces), state)
            myTask.ContinueWith(Sub(antecedent) ac(myTask))

        End Function
        Private Function Compute(ByVal decimalPlaces As Integer)
            Console.WriteLine("Calling compute on thread {0}", Thread.CurrentThread.ManagedThreadId)

            ' Simulating some heavy work.
            Thread.SpinWait(500000000)

            ' Actual implementation left as exercise for the reader.
            ' Several examples are available on the Web.
            Return "3.14159265358979323846264338327950288"
        End Function

        Public Function EndCalculate(ByVal ar As IAsyncResult) As String
            Console.WriteLine("Calling EndCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId)
            Return CType(ar, Task(Of String)).Result
        End Function
    End Class

    Class CalculatorClient
        Shared decimalPlaces As Integer
        Shared Sub Main()
            Dim calc As New Calculator
            Dim places As Integer = 35
            Dim callback As New AsyncCallback(AddressOf PrintResult)
            Dim ar As IAsyncResult = calc.BeginCalculate(places, callback, calc)

            ' Do some work on this thread while the calculator is busy.
            Console.WriteLine("Working...")
            Thread.SpinWait(500000)
            Console.ReadLine()
        End Sub

        Public Shared Sub PrintResult(ByVal result As IAsyncResult)
            Dim c As Calculator = CType(result.AsyncState, Calculator)
            Dim piString As String = c.EndCalculate(result)
            Console.WriteLine("Calling PrintResult on thread {0}; result = {1}",
                       Thread.CurrentThread.ManagedThreadId, piString)
        End Sub

    End Class
    '</snippet09>

    Class FileStreamDemo
        Shared Sub Main()
            Dim fsd As New FileStreamDemo()
            fsd.ShowCallingFromAsync()
        End Sub

        '<snippet03>
        Const MAX_FILE_SIZE As Integer = 14000000
        Shared Function GetFileStringAsync(ByVal path As String) As Task(Of String)
            Dim fi As New FileInfo(path)
            Dim data(fi.Length - 1) As Byte

            Dim fs As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, True)

            ' Task(Of Integer) returns the number of bytes read
            Dim myTask As Task(Of Integer) = Task(Of Integer).Factory.FromAsync(
                AddressOf fs.BeginRead, AddressOf fs.EndRead, data, 0, data.Length, Nothing)

            ' It is possible to do other work here while waiting
            ' for the antecedent task to complete.
            ' ...

            ' Add the continuation, which returns a Task<string>. 
            Return myTask.ContinueWith(Function(antecedent)
                                           fs.Close()
                                           If (antecedent.Result < 100) Then
                                               Return "Data is too small to bother with."
                                           End If
                                           ' If we did not receive the entire file, the end of the
                                           ' data buffer will contain garbage.
                                           If (antecedent.Result < data.Length) Then
                                               Array.Resize(data, antecedent.Result)
                                           End If

                                           ' Will be returned in the Result property of the Task<string>
                                           ' at some future point after the asynchronous file I/O operation completes.
                                           Return New UTF8Encoding().GetString(data)
                                       End Function)

        End Function
        '</snippet03>

        Public Sub ShowCallingFromAsync()
            Dim path As String = "\\docbuild2\public\Main\Logs\DllDiffReport\DllDiffReport_BetweenInBuildAndInPublicShare.html"
            '<snippet04>
            Dim myTask As Task(Of String) = GetFileStringAsync(path)

            ' Do some other work
            ' ...

            Try
                Console.WriteLine(myTask.Result.Substring(0, 500))
            Catch ex As AggregateException
                Console.WriteLine(ex.InnerException.Message)
            End Try
            '</snippet04>

            Console.WriteLine("Press Enter to exit.")
            Console.ReadLine()
        End Sub

        '<snippet05>
        Public Function GetFileStringAsync2(ByVal path As String) As Task(Of String)
            Dim fi = New FileInfo(path)
            Dim data(fi.Length - 1) As Byte
            Dim state As New MyCustomState()

            Dim fs As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, True)
            ' We still pass null for the last parameter because
            ' the state variable is visible to the continuation delegate.
            Dim myTask As Task(Of Integer) = Task(Of Integer).Factory.FromAsync(
                    AddressOf fs.BeginRead, AddressOf fs.EndRead, data, 0, data.Length, Nothing)

            Return myTask.ContinueWith(Function(antecedent)
                                           fs.Close()
                                           ' Capture custom state data directly in the user delegate.
                                           ' No need to pass it through the FromAsync method.
                                           If (state.StateData.Contains("New York, New York")) Then
                                               Return "Start spreading the news!"
                                           End If

                                           ' If we did not receive the entire file, the end of the
                                           ' data buffer will contain garbage.
                                           If (antecedent.Result < data.Length) Then
                                               Array.Resize(data, antecedent.Result)
                                           End If
                                           '/ Will be returned in the Result property of the Task<string>
                                           '/ at some future point after the asynchronous file I/O operation completes.
                                           Return New UTF8Encoding().GetString(data)
                                       End Function)

        End Function
        '</snippet05>


        '<snippet06>
        Public Function GetMultiFileData(ByVal filesToRead As String()) As Task(Of String)
            Dim fs As FileStream
            Dim tasks(filesToRead.Length - 1) As Task(Of String)
            Dim fileData() As Byte = Nothing
            For i As Integer = 0 To filesToRead.Length
                fileData(&H1000) = New Byte()
                fs = New FileStream(filesToRead(i), FileMode.Open, FileAccess.Read, FileShare.Read, fileData.Length, True)

                ' By adding the continuation here, the 
                ' Result of each task will be a string.
                tasks(i) = Task(Of Integer).Factory.FromAsync(AddressOf fs.BeginRead,
                                                              AddressOf fs.EndRead,
                                                              fileData,
                                                              0,
                                                              fileData.Length,
                                                              Nothing).
                                                          ContinueWith(Function(antecedent)
                                                                           fs.Close()
                                                                           'If we did not receive the entire file, the end of the
                                                                           ' data buffer will contain garbage.
                                                                           If (antecedent.Result < fileData.Length) Then
                                                                               ReDim Preserve fileData(antecedent.Result)
                                                                           End If

                                                                           'Will be returned in the Result property of the Task<string>
                                                                           ' at some future point after the asynchronous file I/O operation completes.
                                                                           Return New UTF8Encoding().GetString(fileData)
                                                                       End Function)
            Next

            Return Task(Of String).Factory.ContinueWhenAll(tasks, Function(data)

                                                                      ' Propagate all exceptions and mark all faulted tasks as observed.
                                                                      Task.WaitAll(data)

                                                                      ' Combine the results from all tasks.
                                                                      Dim sb As New StringBuilder()
                                                                      For Each t As Task(Of String) In data
                                                                          sb.Append(t.Result)
                                                                      Next
                                                                      ' Final result to be returned eventually on the calling thread.
                                                                      Return sb.ToString()
                                                                  End Function)
        End Function
        '</snippet06>

        Class MyCustomState
            Public StateData As String
            Public Sub New()
                StateData = "Hello"
            End Sub
        End Class


    End Class


End Module

The method can then be called, as follows.

sample · csharpsamples/snippets/csharp/VS_Snippets_Misc/fromasync/cs/fromasync.cs04

csharp
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net;

namespace specialNamespaceForOneMethodSignature
{
    using System;
    class Task<TResult>
    {
        public TResult TValue;
    }
    class Dummy <TResult>
    {
            // <snippet01>
            public Task<TResult> FromAsync<TArg1, TArg2, TArg3>(
                Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, //BeginRead
                 Func<IAsyncResult, TResult> endMethod, //EndRead
                 TArg1 arg1, // the byte[] buffer
                 TArg2 arg2, // the offset in arg1 at which to start writing data
                 TArg3 arg3, // the maximum number of bytes to read
                 object state // optional state information
                )
                //</snippet01>
        { return new Task<TResult>(); }
    }
}

namespace APM_Task
{
    class TestFileStreamNaive
    {
        //<snippet02>
        public string GetFileData(string path)
        {
            FileInfo fi = new FileInfo(path);
            byte[] data = new byte[fi.Length];
            FileStream fs;

            // a using statement is ok here because we Wait before returning
            // and will be done with the filestream.
            using (fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true))
            {
                //Task<int> returns the number of bytes read
                Task<int> task = Task<int>.Factory.FromAsync(
                        fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

                task.Wait();
                return new UTF8Encoding().GetString(data);
            }
        }
        //</snippet02>
        static void Main()
        {
            TestFileStreamNaive instance = new TestFileStreamNaive();
            string s = instance.GetFileData("testfile.txt");
        }
    }

    class AsyncResult : IAsyncResult
    {

        public AsyncResult() { }
        public object AsyncState
        {
            get { return "Hello from a parallel universe."; }
        }

        public WaitHandle AsyncWaitHandle
        {
            get { throw new NotImplementedException(); }
        }

        public bool CompletedSynchronously
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsCompleted
        {
            get { throw new NotImplementedException(); }
        }

        static IAsyncResult DoSomethingAsynchronously()
        {
            return new AsyncResult();
        }

        //<snippet07>
        static Task<String> ReturnTaskFromAsyncResult()
        {
            IAsyncResult ar = DoSomethingAsynchronously();
            Task<String> t = Task<string>.Factory.FromAsync(ar, _ =>
                {
                    return (string)ar.AsyncState;
                });

            return t;
        }
        //</snippet07>
    }

    //<snippet08>
    class WebDataDownloader
    {

        static void Main()
        {
            WebDataDownloader downloader = new WebDataDownloader();
            string[] addresses = { "http://www.msnbc.com", "http://www.yahoo.com",
                                     "http://www.nytimes.com", "http://www.washingtonpost.com",
                                     "http://www.latimes.com", "http://www.newsday.com" };
            CancellationTokenSource cts = new CancellationTokenSource();

            // Create a UI thread from which to cancel the entire operation
            Task.Factory.StartNew(() =>
            {
                Console.WriteLine("Press c to cancel");
                if (Console.ReadKey().KeyChar == 'c')
                    cts.Cancel();
            });

            // Using a neutral search term that is sure to get some hits.
            Task<string[]> webTask = downloader.GetWordCounts(addresses, "the", cts.Token);

            // Do some other work here unless the method has already completed.
            if (!webTask.IsCompleted)
            {
                // Simulate some work.
                Thread.SpinWait(5000000);
            }

            string[] results = null;
            try
            {
                results = webTask.Result;
            }
            catch (AggregateException e)
            {
                foreach (var ex in e.InnerExceptions)
                {
                    OperationCanceledException oce = ex as OperationCanceledException;
                    if (oce != null)
                    {
                        if (oce.CancellationToken == cts.Token)
                        {
                            Console.WriteLine("Operation canceled by user.");
                        }
                    }
                    else
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            finally
            {
                cts.Dispose();
            }
            if (results != null)
            {
                foreach (var item in results)
                    Console.WriteLine(item);
            }
            Console.ReadKey();
        }

        Task<string[]> GetWordCounts(string[] urls, string name, CancellationToken token)
        {
            TaskCompletionSource<string[]> tcs = new TaskCompletionSource<string[]>();
            WebClient[] webClients = new WebClient[urls.Length];

            // If the user cancels the CancellationToken, then we can use the
            // WebClient's ability to cancel its own async operations.
            token.Register(() =>
                {
                    foreach (var wc in webClients)
                    {
                        if (wc != null)
                            wc.CancelAsync();
                    }
                });

            object m_lock = new object();
            int count = 0;
            List<string> results = new List<string>();
            for (int i = 0; i < urls.Length; i++)
            {
                webClients[i] = new WebClient();

                #region callback
                // Specify the callback for the DownloadStringCompleted
                // event that will be raised by this WebClient instance.
                webClients[i].DownloadStringCompleted += (obj, args) =>
                 {
                     if (args.Cancelled == true)
                     {
                         tcs.TrySetCanceled();
                         return;
                     }
                     else if (args.Error != null)
                     {
                         // Pass through to the underlying Task
                         // any exceptions thrown by the WebClient
                         // during the asynchronous operation.
                         tcs.TrySetException(args.Error);
                         return;
                     }
                     else
                     {
                         // Split the string into an array of words,
                         // then count the number of elements that match
                         // the search term.
                         string[] words = null;
                         words = args.Result.Split(' ');
                         string NAME = name.ToUpper();
                         int nameCount = (from word in words.AsParallel()
                                          where word.ToUpper().Contains(NAME)
                                          select word)
                                         .Count();

                         // Associate the results with the url, and add new string to the array that
                         // the underlying Task object will return in its Result property.
                         results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, name));
                     }

                     // If this is the last async operation to complete,
                     // then set the Result property on the underlying Task.
                     lock (m_lock)
                     {
                         count++;
                         if (count == urls.Length)
                         {
                             tcs.TrySetResult(results.ToArray());
                         }
                     }
                 };
                #endregion

                // Call DownloadStringAsync for each URL.
                Uri address = null;
                try
                {
                    address = new Uri(urls[i]);
                    // Pass the address, and also use it for the userToken
                    // to identify the page when the delegate is invoked.
                    webClients[i].DownloadStringAsync(address, address);
                }

                catch (UriFormatException ex)
                {
                    // Abandon the entire operation if one url is malformed.
                    // Other actions are possible here.
                    tcs.TrySetException(ex);
                    return tcs.Task;
                }
            }

            // Return the underlying Task. The client code
            // waits on the Result property, and handles exceptions
            // in the try-catch block there.
            return tcs.Task;
        }
        //</snippet08>
    }

    #region snippet09
    class Calculator
    {
        public IAsyncResult BeginCalculate(int decimalPlaces, AsyncCallback ac, object state)
        {
            Console.WriteLine($"Calling BeginCalculate on thread {Thread.CurrentThread.ManagedThreadId}");
            Task<string> f = Task<string>.Factory.StartNew(_ => Compute(decimalPlaces), state);
            if (ac != null) f.ContinueWith((res) => ac(f));
            return f;
        }

        public string Compute(int numPlaces)
        {
            Console.WriteLine($"Calling compute on thread {Thread.CurrentThread.ManagedThreadId}");

            // Simulating some heavy work.
            Thread.SpinWait(500000000);

            // Actual implementation left as exercise for the reader.
            // Several examples are available on the Web.
            return "3.14159265358979323846264338327950288";
        }

        public string EndCalculate(IAsyncResult ar)
        {
            Console.WriteLine($"Calling EndCalculate on thread {Thread.CurrentThread.ManagedThreadId}");
            return ((Task<string>)ar).Result;
        }
    }

    public class CalculatorClient
    {
        static int decimalPlaces = 12;
        public static void Main()
        {
            Calculator calc = new Calculator();
            int places = 35;

            AsyncCallback callBack = new AsyncCallback(PrintResult);
            IAsyncResult ar = calc.BeginCalculate(places, callBack, calc);

            // Do some work on this thread while the calculator is busy.
            Console.WriteLine("Working...");
            Thread.SpinWait(500000);
            Console.ReadLine();
        }

        public static void PrintResult(IAsyncResult result)
        {
            Calculator c = (Calculator)result.AsyncState;
            string piString = c.EndCalculate(result);
            Console.WriteLine($"Calling PrintResult on thread {Thread.CurrentThread.ManagedThreadId}; result = {piString}");
        }
    }
    #endregion
} //end APM_Task namespace

class FileStreamDemo
{
    static void Main()
    {
        FileStreamDemo fsd = new FileStreamDemo();
        //  var task = fsd.GetMultiFileData(new string[1]);
         fsd.ShowCallingFromAsync();
       // fsd.ShowCallingFromAsyncFileData();
        //  fsd.ProcessFilesAsync();
        //  task.Wait();
        //  Console.WriteLine(task.Result);

        Console.WriteLine("press any key");
        Console.ReadLine();
    }
    //<snippet03>
    const int MAX_FILE_SIZE = 14000000;
    public static Task<string> GetFileStringAsync(string path)
    {
        FileInfo fi = new FileInfo(path);
        byte[] data = null;
        data = new byte[fi.Length];

        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

        //Task<int> returns the number of bytes read
        Task<int> task = Task<int>.Factory.FromAsync(
                fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

        // It is possible to do other work here while waiting
        // for the antecedent task to complete.
        // ...

        // Add the continuation, which returns a Task<string>.
        return task.ContinueWith((antecedent) =>
        {
            fs.Close();

            // Result = "number of bytes read" (if we need it.)
            if (antecedent.Result < 100)
            {
                return "Data is too small to bother with.";
            }
            else
            {
                // If we did not receive the entire file, the end of the
                // data buffer will contain garbage.
                if (antecedent.Result < data.Length)
                    Array.Resize(ref data, antecedent.Result);

                // Will be returned in the Result property of the Task<string>
                // at some future point after the asynchronous file I/O operation completes.
                return new UTF8Encoding().GetString(data);
            }
        });
    }
    //</snippet03>

        void ShowCallingFromAsync()
        {
            string path = @"\\docbuild2\public\Main\Logs\DllDiffReport\DllDiffReport_BetweenInBuildAndInProduct.html";
            //<snippet04>

            Task<string> t = GetFileStringAsync(path);

            // Do some other work:
            // ...

            try
            {
                 Console.WriteLine(t.Result.Substring(0, 500));
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(ae.InnerException.Message);
            }
            //</snippet04>

            Console.ReadLine();
        }

        //not used???
        //<snippet12>
        public static Task<int> GetFileDataAsync(string path, ref byte[] data)
        {
            // Error handling omitted for brevity...

            FileInfo fi = new FileInfo(path);
            data = new byte[fi.Length];
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

            // Task<int> returns the number of bytes read
            // Pass FileStream in AsyncState parameter in order to close it from the call site.
            return Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, fs);
        }
        //</snippet12>

        void ShowCallingFromAsyncFileData()
        {
            //<snippet13>
            string path = @"\\remoteComputer\filename.txt";

            byte[] buffer = null;
            Task<int> t = GetFileDataAsync(path, ref buffer);

            // Can do some other work here:
            // ...
            for(int i = 0; i < 10; i++)
                Console.Write("Working... ");

            var result =
            t.ContinueWith((antecedent) =>
            {
                // Error handling omitted...

                return new UTF8Encoding().GetString(buffer);
            });

            // Can also do some other work here:
            // ...
            for (int i = 0; i < 10; i++)
                Console.Write("Working more... ");
            try
            {
                t.Wait();
                Console.WriteLine(result.Result.Substring(0, 50));
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(ae.InnerException.Message);
            }
            //</snippet13>

            Console.ReadLine();
        }
        // nested dummy class for snippet0 5 below
        class MyCustomState
        {
            public string StateData { get; set; }
            public MyCustomState() { StateData = "Hello"; }
        }
        private MyCustomState GetCustomState()
        {
            return new MyCustomState();
        }

        //<snippet05>
        public Task<string> GetFileStringAsync2(string path)
        {
            FileInfo fi = new FileInfo(path);
            byte[] data = new byte[fi.Length];
            MyCustomState state = GetCustomState();
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);
            // We still pass null for the last parameter because
            // the state variable is visible to the continuation delegate.
            Task<int> task = Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

            return task.ContinueWith((antecedent) =>
            {
                // It is safe to close the filestream now.
                fs.Close();

                // Capture custom state data directly in the user delegate.
                // No need to pass it through the FromAsync method.
                if (state.StateData.Contains("New York, New York"))
                {
                    return "Start spreading the news!";
                }
                else
                {
                    // If we did not receive the entire file, the end of the
                    // data buffer will contain garbage.
                    if (antecedent.Result < data.Length)
                        Array.Resize(ref data, antecedent.Result);

                    // Will be returned in the Result property of the Task<string>
                    // at some future point after the asynchronous file I/O operation completes.
                    return new UTF8Encoding().GetString(data);
                }
            });
        }
        //</snippet05>

        string[] GetFilesToRead()
        {
            return new string[3] { "test.txt", "test1.txt", "test2.txt" };
        }
        //<snippet06>
        public Task<string> GetMultiFileData(string[] filesToRead)
        {
            FileStream fs;
            Task<string>[] tasks = new Task<string>[filesToRead.Length];
            byte[] fileData = null;
            for (int i = 0; i < filesToRead.Length; i++)
            {
                fileData = new byte[0x1000];
                fs = new FileStream(filesToRead[i], FileMode.Open, FileAccess.Read, FileShare.Read, fileData.Length, true);

                // By adding the continuation here, the
                // Result of each task will be a string.
                tasks[i] = Task<int>.Factory.FromAsync(
                         fs.BeginRead, fs.EndRead, fileData, 0, fileData.Length, null)
                         .ContinueWith((antecedent) =>
                             {
                                 fs.Close();

                                 // If we did not receive the entire file, the end of the
                                 // data buffer will contain garbage.
                                 if (antecedent.Result < fileData.Length)
                                     Array.Resize(ref fileData, antecedent.Result);

                                 // Will be returned in the Result property of the Task<string>
                                 // at some future point after the asynchronous file I/O operation completes.
                                 return new UTF8Encoding().GetString(fileData);
                             });
            }

            // Wait for all tasks to complete.
            return Task<string>.Factory.ContinueWhenAll(tasks, (data) =>
            {
                // Propagate all exceptions and mark all faulted tasks as observed.
                Task.WaitAll(data);

                // Combine the results from all tasks.
                StringBuilder sb = new StringBuilder();
                foreach (var t in data)
                {
                    sb.Append(t.Result);
                }
                // Final result to be returned eventually on the calling thread.
                return sb.ToString();
            });
        }
        //</snippet06>
        public string ProcessData(Task[] arr)
        {
            return "processed";
        }

        //<snippet11>
        void ProcessFilesAsync()
        {
            string[] files = Directory.GetFiles(@"\\remoteComputer\folderName\", "*.*", SearchOption.AllDirectories);
            CancellationTokenSource cts = new CancellationTokenSource();

            List<Task<string>> tasks = new List<Task<string>>();

            foreach (var file in files)
            {
                Task<string> t = null;
                try
                {
                    t = GetFileStringAsync3(file, cts.Token);
                }
                catch
                {
                    Console.WriteLine($"Argument exception on {file}");
                    continue;
                }
                t.ContinueWith((antecedent) => ProcessFileData(antecedent.Result));
                tasks.Add(t);
            }
            Console.WriteLine("All tasks have been added.");
            // Do some work here.

            try
            {
                Task.WaitAll(tasks.ToArray());
                Console.WriteLine("All tasks have completed.");
            }
            catch (AggregateException ae)
            {
                foreach (var ex in ae.InnerExceptions)
                    Console.WriteLine(ex.Message);
            }
            finally
            {
                cts.Dispose();
            }

            // If needed, perform a final step after all tasks have completed.
            Task.Factory.ContinueWhenAll(tasks.ToArray(), (arr) =>
                {
                    foreach (var t in arr)
                    {
                        CompareFileData(t.Result);
                    }
                });
        }

        public Task<string> GetFileStringAsync3(string path, CancellationToken token)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(String.Format("Could not find {0}", path));
            }
            FileInfo fi = new FileInfo(path);
            byte[] data = null;

            if (fi.Length < MAX_FILE_SIZE && fi.Length > 0)
        {
            data = new byte[fi.Length];
        }
        else
            {
                throw new ArgumentException(String.Format("{0} is too big to load!", path));
            }

            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

            // We still pass null for the last parameter because
            // the state variable is visible to the continuation delegate.
            Task<int> task = Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

            return task.ContinueWith((antecedent) =>
            {
                fs.Close();

                // If we did not receive the entire file, the end of the
                // data buffer will contain garbage.
                if (antecedent.Result < data.Length)
                    Array.Resize(ref data, antecedent.Result);

                // Will be returned in the Result property of the Task<string>
                // at some future point after the asynchronous file I/O operation completes.
                return new UTF8Encoding().GetString(data);
            });
        }

        string ProcessFileData(string data)
        {

            string s = data.Substring(0, 80);
            Console.WriteLine(s);
            return s;
        }

        string CompareFileData(string s)
        {
            return "compare results";
        }
        //</snippet11>
    }

sample · vbsamples/snippets/visualbasic/VS_Snippets_Misc/fromasync/vb/module1.vb04

vb
Imports System.Collections.Generic
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.Threading.Tasks

Namespace specialNamespaceForOneMethodSignature
    Class Task(Of TResult)
        Public TValue As TResult
    End Class
    Class Dummy(Of TResult)
        '<snippet01>
        Public Function FromAsync(Of TArg1, TArg2, TArg3)(
                        ByVal beginMethod As Func(Of TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult),
                        ByVal endMethod As Func(Of IAsyncResult, TResult),
                        ByVal dataBuffer As TArg1,
                        ByVal byteOffsetToStartAt As TArg2,
                        ByVal maxBytesToRead As TArg3,
                        ByVal stateInfo As Object)
            '</snippet01>
            Return New Task(Of TResult)
        End Function


    End Class
End Namespace

Module Module1

    Class AsyncResult
        Public Sub New()

        End Sub
        '<snippet07>
        Shared Function ReturnTaskFromAsyncResult() As Task(Of String)
            Dim ar As IAsyncResult = DoSomethingAsynchronously()
            Dim t As Task(Of String) = Task(Of String).Factory.FromAsync(ar, Function(res) CStr(res.AsyncState))
            Return t
        End Function
        '</snippet07>

        Private Shared Function DoSomethingAsynchronously() As IAsyncResult
            Throw New NotImplementedException
        End Function

    End Class
    '<snippet08>
    Class WebDataDownLoader

        Dim tcs As New TaskCompletionSource(Of String())
        Dim nameToSearch As String
        Dim token As CancellationToken
        Dim results As New List(Of String)
        Dim m_lock As Object
        Dim count As Integer
        Dim addresses() As String

        Shared Sub Main()

            Dim downloader As New WebDataDownLoader()
            downloader.addresses = {"http://www.msnbc.com", "http://www.yahoo.com", _
                                         "http://www.nytimes.com", "http://www.washingtonpost.com", _
                                         "http://www.latimes.com", "http://www.newsday.com"}
            Dim cts As New CancellationTokenSource()

            ' Create a UI thread from which to cancel the entire operation
            Task.Factory.StartNew(Sub()
                                      Console.WriteLine("Press c to cancel")
                                      If Console.ReadKey().KeyChar = "c"c Then
                                          cts.Cancel()
                                      End If
                                  End Sub)

            ' Using a neutral search term that is sure to get some hits on English web sites.
            ' Please substitute your favorite search term.
            downloader.nameToSearch = "the"
            Dim webTask As Task(Of String()) = downloader.GetWordCounts(downloader.addresses, downloader.nameToSearch, cts.Token)

            ' Do some other work here unless the method has already completed.
            If (webTask.IsCompleted = False) Then
                ' Simulate some work
                Thread.SpinWait(5000000)
            End If

            Dim results As String() = Nothing
            Try
                results = webTask.Result
            Catch ae As AggregateException
                For Each ex As Exception In ae.InnerExceptions
                    If (TypeOf (ex) Is OperationCanceledException) Then
                        Dim oce As OperationCanceledException = CType(ex, OperationCanceledException)
                        If oce.CancellationToken = cts.Token Then
                            Console.WriteLine("Operation canceled by user.")
                        End If
                    Else
                        Console.WriteLine(ex.Message)
                    End If

                Next
            Finally
                cts.Dispose()
            End Try

            If (Not results Is Nothing) Then
                For Each item As String In results
                    Console.WriteLine(item)
                Next
            End If

            Console.WriteLine("Press any key to exit")
            Console.ReadKey()
        End Sub

        Public Function GetWordCounts(ByVal urls() As String, ByVal str As String, ByVal token As CancellationToken) As Task(Of String())

            Dim webClients() As WebClient
            ReDim webClients(urls.Length)
            m_lock = New Object()

            ' If the user cancels the CancellationToken, then we can use the
            ' WebClient's ability to cancel its own async operations.
            token.Register(Sub()
                               For Each wc As WebClient In webClients
                                   If Not wc Is Nothing Then
                                       wc.CancelAsync()
                                   End If
                               Next
                           End Sub)


            For i As Integer = 0 To urls.Length - 1
                webClients(i) = New WebClient()

                ' Specify the callback for the DownloadStringCompleted
                ' event that will be raised by this WebClient instance.
                AddHandler webClients(i).DownloadStringCompleted, AddressOf WebEventHandler

                Dim address As Uri = Nothing
                Try
                    address = New Uri(urls(i))
                    ' Pass the address, and also use it for the userToken 
                    ' to identify the page when the delegate is invoked.
                    webClients(i).DownloadStringAsync(address, address)
                Catch ex As UriFormatException
                    tcs.TrySetException(ex)
                    Return tcs.Task
                End Try

            Next

            ' Return the underlying Task. The client code
            ' waits on the Result property, and handles exceptions
            ' in the try-catch block there.
            Return tcs.Task
        End Function



        Public Sub WebEventHandler(ByVal sender As Object, ByVal args As DownloadStringCompletedEventArgs)

            If args.Cancelled = True Then
                tcs.TrySetCanceled()
                Return
            ElseIf Not args.Error Is Nothing Then
                tcs.TrySetException(args.Error)
                Return
            Else
                ' Split the string into an array of words,
                ' then count the number of elements that match
                ' the search term.
                Dim words() As String = args.Result.Split(" "c)
                Dim NAME As String = nameToSearch.ToUpper()
                Dim nameCount = (From word In words.AsParallel()
                                 Where word.ToUpper().Contains(NAME)
                                 Select word).Count()

                ' Associate the results with the url, and add new string to the array that 
                ' the underlying Task object will return in its Result property.
                results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, nameToSearch))
            End If

            SyncLock (m_lock)
                count = count + 1
                If (count = addresses.Length) Then
                    tcs.TrySetResult(results.ToArray())
                End If
            End SyncLock
        End Sub
        '</snippet08>
    End Class


    '<snippet09>
    Class Calculator
        Public Function BeginCalculate(ByVal decimalPlaces As Integer, ByVal ac As AsyncCallback, ByVal state As Object) As IAsyncResult
            Console.WriteLine("Calling BeginCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId)
            Dim myTask = Task(Of String).Factory.StartNew(Function(obj) Compute(decimalPlaces), state)
            myTask.ContinueWith(Sub(antecedent) ac(myTask))

        End Function
        Private Function Compute(ByVal decimalPlaces As Integer)
            Console.WriteLine("Calling compute on thread {0}", Thread.CurrentThread.ManagedThreadId)

            ' Simulating some heavy work.
            Thread.SpinWait(500000000)

            ' Actual implementation left as exercise for the reader.
            ' Several examples are available on the Web.
            Return "3.14159265358979323846264338327950288"
        End Function

        Public Function EndCalculate(ByVal ar As IAsyncResult) As String
            Console.WriteLine("Calling EndCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId)
            Return CType(ar, Task(Of String)).Result
        End Function
    End Class

    Class CalculatorClient
        Shared decimalPlaces As Integer
        Shared Sub Main()
            Dim calc As New Calculator
            Dim places As Integer = 35
            Dim callback As New AsyncCallback(AddressOf PrintResult)
            Dim ar As IAsyncResult = calc.BeginCalculate(places, callback, calc)

            ' Do some work on this thread while the calculator is busy.
            Console.WriteLine("Working...")
            Thread.SpinWait(500000)
            Console.ReadLine()
        End Sub

        Public Shared Sub PrintResult(ByVal result As IAsyncResult)
            Dim c As Calculator = CType(result.AsyncState, Calculator)
            Dim piString As String = c.EndCalculate(result)
            Console.WriteLine("Calling PrintResult on thread {0}; result = {1}",
                       Thread.CurrentThread.ManagedThreadId, piString)
        End Sub

    End Class
    '</snippet09>

    Class FileStreamDemo
        Shared Sub Main()
            Dim fsd As New FileStreamDemo()
            fsd.ShowCallingFromAsync()
        End Sub

        '<snippet03>
        Const MAX_FILE_SIZE As Integer = 14000000
        Shared Function GetFileStringAsync(ByVal path As String) As Task(Of String)
            Dim fi As New FileInfo(path)
            Dim data(fi.Length - 1) As Byte

            Dim fs As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, True)

            ' Task(Of Integer) returns the number of bytes read
            Dim myTask As Task(Of Integer) = Task(Of Integer).Factory.FromAsync(
                AddressOf fs.BeginRead, AddressOf fs.EndRead, data, 0, data.Length, Nothing)

            ' It is possible to do other work here while waiting
            ' for the antecedent task to complete.
            ' ...

            ' Add the continuation, which returns a Task<string>. 
            Return myTask.ContinueWith(Function(antecedent)
                                           fs.Close()
                                           If (antecedent.Result < 100) Then
                                               Return "Data is too small to bother with."
                                           End If
                                           ' If we did not receive the entire file, the end of the
                                           ' data buffer will contain garbage.
                                           If (antecedent.Result < data.Length) Then
                                               Array.Resize(data, antecedent.Result)
                                           End If

                                           ' Will be returned in the Result property of the Task<string>
                                           ' at some future point after the asynchronous file I/O operation completes.
                                           Return New UTF8Encoding().GetString(data)
                                       End Function)

        End Function
        '</snippet03>

        Public Sub ShowCallingFromAsync()
            Dim path As String = "\\docbuild2\public\Main\Logs\DllDiffReport\DllDiffReport_BetweenInBuildAndInPublicShare.html"
            '<snippet04>
            Dim myTask As Task(Of String) = GetFileStringAsync(path)

            ' Do some other work
            ' ...

            Try
                Console.WriteLine(myTask.Result.Substring(0, 500))
            Catch ex As AggregateException
                Console.WriteLine(ex.InnerException.Message)
            End Try
            '</snippet04>

            Console.WriteLine("Press Enter to exit.")
            Console.ReadLine()
        End Sub

        '<snippet05>
        Public Function GetFileStringAsync2(ByVal path As String) As Task(Of String)
            Dim fi = New FileInfo(path)
            Dim data(fi.Length - 1) As Byte
            Dim state As New MyCustomState()

            Dim fs As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, True)
            ' We still pass null for the last parameter because
            ' the state variable is visible to the continuation delegate.
            Dim myTask As Task(Of Integer) = Task(Of Integer).Factory.FromAsync(
                    AddressOf fs.BeginRead, AddressOf fs.EndRead, data, 0, data.Length, Nothing)

            Return myTask.ContinueWith(Function(antecedent)
                                           fs.Close()
                                           ' Capture custom state data directly in the user delegate.
                                           ' No need to pass it through the FromAsync method.
                                           If (state.StateData.Contains("New York, New York")) Then
                                               Return "Start spreading the news!"
                                           End If

                                           ' If we did not receive the entire file, the end of the
                                           ' data buffer will contain garbage.
                                           If (antecedent.Result < data.Length) Then
                                               Array.Resize(data, antecedent.Result)
                                           End If
                                           '/ Will be returned in the Result property of the Task<string>
                                           '/ at some future point after the asynchronous file I/O operation completes.
                                           Return New UTF8Encoding().GetString(data)
                                       End Function)

        End Function
        '</snippet05>


        '<snippet06>
        Public Function GetMultiFileData(ByVal filesToRead As String()) As Task(Of String)
            Dim fs As FileStream
            Dim tasks(filesToRead.Length - 1) As Task(Of String)
            Dim fileData() As Byte = Nothing
            For i As Integer = 0 To filesToRead.Length
                fileData(&H1000) = New Byte()
                fs = New FileStream(filesToRead(i), FileMode.Open, FileAccess.Read, FileShare.Read, fileData.Length, True)

                ' By adding the continuation here, the 
                ' Result of each task will be a string.
                tasks(i) = Task(Of Integer).Factory.FromAsync(AddressOf fs.BeginRead,
                                                              AddressOf fs.EndRead,
                                                              fileData,
                                                              0,
                                                              fileData.Length,
                                                              Nothing).
                                                          ContinueWith(Function(antecedent)
                                                                           fs.Close()
                                                                           'If we did not receive the entire file, the end of the
                                                                           ' data buffer will contain garbage.
                                                                           If (antecedent.Result < fileData.Length) Then
                                                                               ReDim Preserve fileData(antecedent.Result)
                                                                           End If

                                                                           'Will be returned in the Result property of the Task<string>
                                                                           ' at some future point after the asynchronous file I/O operation completes.
                                                                           Return New UTF8Encoding().GetString(fileData)
                                                                       End Function)
            Next

            Return Task(Of String).Factory.ContinueWhenAll(tasks, Function(data)

                                                                      ' Propagate all exceptions and mark all faulted tasks as observed.
                                                                      Task.WaitAll(data)

                                                                      ' Combine the results from all tasks.
                                                                      Dim sb As New StringBuilder()
                                                                      For Each t As Task(Of String) In data
                                                                          sb.Append(t.Result)
                                                                      Next
                                                                      ' Final result to be returned eventually on the calling thread.
                                                                      Return sb.ToString()
                                                                  End Function)
        End Function
        '</snippet06>

        Class MyCustomState
            Public StateData As String
            Public Sub New()
                StateData = "Hello"
            End Sub
        End Class


    End Class


End Module

Provide custom state data

In typical System.IAsyncResult operations, if your System.AsyncCallback delegate requires some custom state data, you have to pass it in through the last parameter in the Begin method, so that the data can be packaged into the System.IAsyncResult object that is eventually passed to the callback method. This is typically not required when the FromAsync methods are used. If the custom data is known to the continuation, then it can be captured directly in the continuation delegate. The following example resembles the previous example, but instead of examining the Result property of the antecedent, the continuation examines the custom state data that is directly accessible to the user delegate of the continuation.

sample · csharpsamples/snippets/csharp/VS_Snippets_Misc/fromasync/cs/fromasync.cs05

csharp
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net;

namespace specialNamespaceForOneMethodSignature
{
    using System;
    class Task<TResult>
    {
        public TResult TValue;
    }
    class Dummy <TResult>
    {
            // <snippet01>
            public Task<TResult> FromAsync<TArg1, TArg2, TArg3>(
                Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, //BeginRead
                 Func<IAsyncResult, TResult> endMethod, //EndRead
                 TArg1 arg1, // the byte[] buffer
                 TArg2 arg2, // the offset in arg1 at which to start writing data
                 TArg3 arg3, // the maximum number of bytes to read
                 object state // optional state information
                )
                //</snippet01>
        { return new Task<TResult>(); }
    }
}

namespace APM_Task
{
    class TestFileStreamNaive
    {
        //<snippet02>
        public string GetFileData(string path)
        {
            FileInfo fi = new FileInfo(path);
            byte[] data = new byte[fi.Length];
            FileStream fs;

            // a using statement is ok here because we Wait before returning
            // and will be done with the filestream.
            using (fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true))
            {
                //Task<int> returns the number of bytes read
                Task<int> task = Task<int>.Factory.FromAsync(
                        fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

                task.Wait();
                return new UTF8Encoding().GetString(data);
            }
        }
        //</snippet02>
        static void Main()
        {
            TestFileStreamNaive instance = new TestFileStreamNaive();
            string s = instance.GetFileData("testfile.txt");
        }
    }

    class AsyncResult : IAsyncResult
    {

        public AsyncResult() { }
        public object AsyncState
        {
            get { return "Hello from a parallel universe."; }
        }

        public WaitHandle AsyncWaitHandle
        {
            get { throw new NotImplementedException(); }
        }

        public bool CompletedSynchronously
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsCompleted
        {
            get { throw new NotImplementedException(); }
        }

        static IAsyncResult DoSomethingAsynchronously()
        {
            return new AsyncResult();
        }

        //<snippet07>
        static Task<String> ReturnTaskFromAsyncResult()
        {
            IAsyncResult ar = DoSomethingAsynchronously();
            Task<String> t = Task<string>.Factory.FromAsync(ar, _ =>
                {
                    return (string)ar.AsyncState;
                });

            return t;
        }
        //</snippet07>
    }

    //<snippet08>
    class WebDataDownloader
    {

        static void Main()
        {
            WebDataDownloader downloader = new WebDataDownloader();
            string[] addresses = { "http://www.msnbc.com", "http://www.yahoo.com",
                                     "http://www.nytimes.com", "http://www.washingtonpost.com",
                                     "http://www.latimes.com", "http://www.newsday.com" };
            CancellationTokenSource cts = new CancellationTokenSource();

            // Create a UI thread from which to cancel the entire operation
            Task.Factory.StartNew(() =>
            {
                Console.WriteLine("Press c to cancel");
                if (Console.ReadKey().KeyChar == 'c')
                    cts.Cancel();
            });

            // Using a neutral search term that is sure to get some hits.
            Task<string[]> webTask = downloader.GetWordCounts(addresses, "the", cts.Token);

            // Do some other work here unless the method has already completed.
            if (!webTask.IsCompleted)
            {
                // Simulate some work.
                Thread.SpinWait(5000000);
            }

            string[] results = null;
            try
            {
                results = webTask.Result;
            }
            catch (AggregateException e)
            {
                foreach (var ex in e.InnerExceptions)
                {
                    OperationCanceledException oce = ex as OperationCanceledException;
                    if (oce != null)
                    {
                        if (oce.CancellationToken == cts.Token)
                        {
                            Console.WriteLine("Operation canceled by user.");
                        }
                    }
                    else
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            finally
            {
                cts.Dispose();
            }
            if (results != null)
            {
                foreach (var item in results)
                    Console.WriteLine(item);
            }
            Console.ReadKey();
        }

        Task<string[]> GetWordCounts(string[] urls, string name, CancellationToken token)
        {
            TaskCompletionSource<string[]> tcs = new TaskCompletionSource<string[]>();
            WebClient[] webClients = new WebClient[urls.Length];

            // If the user cancels the CancellationToken, then we can use the
            // WebClient's ability to cancel its own async operations.
            token.Register(() =>
                {
                    foreach (var wc in webClients)
                    {
                        if (wc != null)
                            wc.CancelAsync();
                    }
                });

            object m_lock = new object();
            int count = 0;
            List<string> results = new List<string>();
            for (int i = 0; i < urls.Length; i++)
            {
                webClients[i] = new WebClient();

                #region callback
                // Specify the callback for the DownloadStringCompleted
                // event that will be raised by this WebClient instance.
                webClients[i].DownloadStringCompleted += (obj, args) =>
                 {
                     if (args.Cancelled == true)
                     {
                         tcs.TrySetCanceled();
                         return;
                     }
                     else if (args.Error != null)
                     {
                         // Pass through to the underlying Task
                         // any exceptions thrown by the WebClient
                         // during the asynchronous operation.
                         tcs.TrySetException(args.Error);
                         return;
                     }
                     else
                     {
                         // Split the string into an array of words,
                         // then count the number of elements that match
                         // the search term.
                         string[] words = null;
                         words = args.Result.Split(' ');
                         string NAME = name.ToUpper();
                         int nameCount = (from word in words.AsParallel()
                                          where word.ToUpper().Contains(NAME)
                                          select word)
                                         .Count();

                         // Associate the results with the url, and add new string to the array that
                         // the underlying Task object will return in its Result property.
                         results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, name));
                     }

                     // If this is the last async operation to complete,
                     // then set the Result property on the underlying Task.
                     lock (m_lock)
                     {
                         count++;
                         if (count == urls.Length)
                         {
                             tcs.TrySetResult(results.ToArray());
                         }
                     }
                 };
                #endregion

                // Call DownloadStringAsync for each URL.
                Uri address = null;
                try
                {
                    address = new Uri(urls[i]);
                    // Pass the address, and also use it for the userToken
                    // to identify the page when the delegate is invoked.
                    webClients[i].DownloadStringAsync(address, address);
                }

                catch (UriFormatException ex)
                {
                    // Abandon the entire operation if one url is malformed.
                    // Other actions are possible here.
                    tcs.TrySetException(ex);
                    return tcs.Task;
                }
            }

            // Return the underlying Task. The client code
            // waits on the Result property, and handles exceptions
            // in the try-catch block there.
            return tcs.Task;
        }
        //</snippet08>
    }

    #region snippet09
    class Calculator
    {
        public IAsyncResult BeginCalculate(int decimalPlaces, AsyncCallback ac, object state)
        {
            Console.WriteLine($"Calling BeginCalculate on thread {Thread.CurrentThread.ManagedThreadId}");
            Task<string> f = Task<string>.Factory.StartNew(_ => Compute(decimalPlaces), state);
            if (ac != null) f.ContinueWith((res) => ac(f));
            return f;
        }

        public string Compute(int numPlaces)
        {
            Console.WriteLine($"Calling compute on thread {Thread.CurrentThread.ManagedThreadId}");

            // Simulating some heavy work.
            Thread.SpinWait(500000000);

            // Actual implementation left as exercise for the reader.
            // Several examples are available on the Web.
            return "3.14159265358979323846264338327950288";
        }

        public string EndCalculate(IAsyncResult ar)
        {
            Console.WriteLine($"Calling EndCalculate on thread {Thread.CurrentThread.ManagedThreadId}");
            return ((Task<string>)ar).Result;
        }
    }

    public class CalculatorClient
    {
        static int decimalPlaces = 12;
        public static void Main()
        {
            Calculator calc = new Calculator();
            int places = 35;

            AsyncCallback callBack = new AsyncCallback(PrintResult);
            IAsyncResult ar = calc.BeginCalculate(places, callBack, calc);

            // Do some work on this thread while the calculator is busy.
            Console.WriteLine("Working...");
            Thread.SpinWait(500000);
            Console.ReadLine();
        }

        public static void PrintResult(IAsyncResult result)
        {
            Calculator c = (Calculator)result.AsyncState;
            string piString = c.EndCalculate(result);
            Console.WriteLine($"Calling PrintResult on thread {Thread.CurrentThread.ManagedThreadId}; result = {piString}");
        }
    }
    #endregion
} //end APM_Task namespace

class FileStreamDemo
{
    static void Main()
    {
        FileStreamDemo fsd = new FileStreamDemo();
        //  var task = fsd.GetMultiFileData(new string[1]);
         fsd.ShowCallingFromAsync();
       // fsd.ShowCallingFromAsyncFileData();
        //  fsd.ProcessFilesAsync();
        //  task.Wait();
        //  Console.WriteLine(task.Result);

        Console.WriteLine("press any key");
        Console.ReadLine();
    }
    //<snippet03>
    const int MAX_FILE_SIZE = 14000000;
    public static Task<string> GetFileStringAsync(string path)
    {
        FileInfo fi = new FileInfo(path);
        byte[] data = null;
        data = new byte[fi.Length];

        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

        //Task<int> returns the number of bytes read
        Task<int> task = Task<int>.Factory.FromAsync(
                fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

        // It is possible to do other work here while waiting
        // for the antecedent task to complete.
        // ...

        // Add the continuation, which returns a Task<string>.
        return task.ContinueWith((antecedent) =>
        {
            fs.Close();

            // Result = "number of bytes read" (if we need it.)
            if (antecedent.Result < 100)
            {
                return "Data is too small to bother with.";
            }
            else
            {
                // If we did not receive the entire file, the end of the
                // data buffer will contain garbage.
                if (antecedent.Result < data.Length)
                    Array.Resize(ref data, antecedent.Result);

                // Will be returned in the Result property of the Task<string>
                // at some future point after the asynchronous file I/O operation completes.
                return new UTF8Encoding().GetString(data);
            }
        });
    }
    //</snippet03>

        void ShowCallingFromAsync()
        {
            string path = @"\\docbuild2\public\Main\Logs\DllDiffReport\DllDiffReport_BetweenInBuildAndInProduct.html";
            //<snippet04>

            Task<string> t = GetFileStringAsync(path);

            // Do some other work:
            // ...

            try
            {
                 Console.WriteLine(t.Result.Substring(0, 500));
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(ae.InnerException.Message);
            }
            //</snippet04>

            Console.ReadLine();
        }

        //not used???
        //<snippet12>
        public static Task<int> GetFileDataAsync(string path, ref byte[] data)
        {
            // Error handling omitted for brevity...

            FileInfo fi = new FileInfo(path);
            data = new byte[fi.Length];
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

            // Task<int> returns the number of bytes read
            // Pass FileStream in AsyncState parameter in order to close it from the call site.
            return Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, fs);
        }
        //</snippet12>

        void ShowCallingFromAsyncFileData()
        {
            //<snippet13>
            string path = @"\\remoteComputer\filename.txt";

            byte[] buffer = null;
            Task<int> t = GetFileDataAsync(path, ref buffer);

            // Can do some other work here:
            // ...
            for(int i = 0; i < 10; i++)
                Console.Write("Working... ");

            var result =
            t.ContinueWith((antecedent) =>
            {
                // Error handling omitted...

                return new UTF8Encoding().GetString(buffer);
            });

            // Can also do some other work here:
            // ...
            for (int i = 0; i < 10; i++)
                Console.Write("Working more... ");
            try
            {
                t.Wait();
                Console.WriteLine(result.Result.Substring(0, 50));
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(ae.InnerException.Message);
            }
            //</snippet13>

            Console.ReadLine();
        }
        // nested dummy class for snippet0 5 below
        class MyCustomState
        {
            public string StateData { get; set; }
            public MyCustomState() { StateData = "Hello"; }
        }
        private MyCustomState GetCustomState()
        {
            return new MyCustomState();
        }

        //<snippet05>
        public Task<string> GetFileStringAsync2(string path)
        {
            FileInfo fi = new FileInfo(path);
            byte[] data = new byte[fi.Length];
            MyCustomState state = GetCustomState();
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);
            // We still pass null for the last parameter because
            // the state variable is visible to the continuation delegate.
            Task<int> task = Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

            return task.ContinueWith((antecedent) =>
            {
                // It is safe to close the filestream now.
                fs.Close();

                // Capture custom state data directly in the user delegate.
                // No need to pass it through the FromAsync method.
                if (state.StateData.Contains("New York, New York"))
                {
                    return "Start spreading the news!";
                }
                else
                {
                    // If we did not receive the entire file, the end of the
                    // data buffer will contain garbage.
                    if (antecedent.Result < data.Length)
                        Array.Resize(ref data, antecedent.Result);

                    // Will be returned in the Result property of the Task<string>
                    // at some future point after the asynchronous file I/O operation completes.
                    return new UTF8Encoding().GetString(data);
                }
            });
        }
        //</snippet05>

        string[] GetFilesToRead()
        {
            return new string[3] { "test.txt", "test1.txt", "test2.txt" };
        }
        //<snippet06>
        public Task<string> GetMultiFileData(string[] filesToRead)
        {
            FileStream fs;
            Task<string>[] tasks = new Task<string>[filesToRead.Length];
            byte[] fileData = null;
            for (int i = 0; i < filesToRead.Length; i++)
            {
                fileData = new byte[0x1000];
                fs = new FileStream(filesToRead[i], FileMode.Open, FileAccess.Read, FileShare.Read, fileData.Length, true);

                // By adding the continuation here, the
                // Result of each task will be a string.
                tasks[i] = Task<int>.Factory.FromAsync(
                         fs.BeginRead, fs.EndRead, fileData, 0, fileData.Length, null)
                         .ContinueWith((antecedent) =>
                             {
                                 fs.Close();

                                 // If we did not receive the entire file, the end of the
                                 // data buffer will contain garbage.
                                 if (antecedent.Result < fileData.Length)
                                     Array.Resize(ref fileData, antecedent.Result);

                                 // Will be returned in the Result property of the Task<string>
                                 // at some future point after the asynchronous file I/O operation completes.
                                 return new UTF8Encoding().GetString(fileData);
                             });
            }

            // Wait for all tasks to complete.
            return Task<string>.Factory.ContinueWhenAll(tasks, (data) =>
            {
                // Propagate all exceptions and mark all faulted tasks as observed.
                Task.WaitAll(data);

                // Combine the results from all tasks.
                StringBuilder sb = new StringBuilder();
                foreach (var t in data)
                {
                    sb.Append(t.Result);
                }
                // Final result to be returned eventually on the calling thread.
                return sb.ToString();
            });
        }
        //</snippet06>
        public string ProcessData(Task[] arr)
        {
            return "processed";
        }

        //<snippet11>
        void ProcessFilesAsync()
        {
            string[] files = Directory.GetFiles(@"\\remoteComputer\folderName\", "*.*", SearchOption.AllDirectories);
            CancellationTokenSource cts = new CancellationTokenSource();

            List<Task<string>> tasks = new List<Task<string>>();

            foreach (var file in files)
            {
                Task<string> t = null;
                try
                {
                    t = GetFileStringAsync3(file, cts.Token);
                }
                catch
                {
                    Console.WriteLine($"Argument exception on {file}");
                    continue;
                }
                t.ContinueWith((antecedent) => ProcessFileData(antecedent.Result));
                tasks.Add(t);
            }
            Console.WriteLine("All tasks have been added.");
            // Do some work here.

            try
            {
                Task.WaitAll(tasks.ToArray());
                Console.WriteLine("All tasks have completed.");
            }
            catch (AggregateException ae)
            {
                foreach (var ex in ae.InnerExceptions)
                    Console.WriteLine(ex.Message);
            }
            finally
            {
                cts.Dispose();
            }

            // If needed, perform a final step after all tasks have completed.
            Task.Factory.ContinueWhenAll(tasks.ToArray(), (arr) =>
                {
                    foreach (var t in arr)
                    {
                        CompareFileData(t.Result);
                    }
                });
        }

        public Task<string> GetFileStringAsync3(string path, CancellationToken token)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(String.Format("Could not find {0}", path));
            }
            FileInfo fi = new FileInfo(path);
            byte[] data = null;

            if (fi.Length < MAX_FILE_SIZE && fi.Length > 0)
        {
            data = new byte[fi.Length];
        }
        else
            {
                throw new ArgumentException(String.Format("{0} is too big to load!", path));
            }

            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

            // We still pass null for the last parameter because
            // the state variable is visible to the continuation delegate.
            Task<int> task = Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

            return task.ContinueWith((antecedent) =>
            {
                fs.Close();

                // If we did not receive the entire file, the end of the
                // data buffer will contain garbage.
                if (antecedent.Result < data.Length)
                    Array.Resize(ref data, antecedent.Result);

                // Will be returned in the Result property of the Task<string>
                // at some future point after the asynchronous file I/O operation completes.
                return new UTF8Encoding().GetString(data);
            });
        }

        string ProcessFileData(string data)
        {

            string s = data.Substring(0, 80);
            Console.WriteLine(s);
            return s;
        }

        string CompareFileData(string s)
        {
            return "compare results";
        }
        //</snippet11>
    }

sample · vbsamples/snippets/visualbasic/VS_Snippets_Misc/fromasync/vb/module1.vb05

vb
Imports System.Collections.Generic
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.Threading.Tasks

Namespace specialNamespaceForOneMethodSignature
    Class Task(Of TResult)
        Public TValue As TResult
    End Class
    Class Dummy(Of TResult)
        '<snippet01>
        Public Function FromAsync(Of TArg1, TArg2, TArg3)(
                        ByVal beginMethod As Func(Of TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult),
                        ByVal endMethod As Func(Of IAsyncResult, TResult),
                        ByVal dataBuffer As TArg1,
                        ByVal byteOffsetToStartAt As TArg2,
                        ByVal maxBytesToRead As TArg3,
                        ByVal stateInfo As Object)
            '</snippet01>
            Return New Task(Of TResult)
        End Function


    End Class
End Namespace

Module Module1

    Class AsyncResult
        Public Sub New()

        End Sub
        '<snippet07>
        Shared Function ReturnTaskFromAsyncResult() As Task(Of String)
            Dim ar As IAsyncResult = DoSomethingAsynchronously()
            Dim t As Task(Of String) = Task(Of String).Factory.FromAsync(ar, Function(res) CStr(res.AsyncState))
            Return t
        End Function
        '</snippet07>

        Private Shared Function DoSomethingAsynchronously() As IAsyncResult
            Throw New NotImplementedException
        End Function

    End Class
    '<snippet08>
    Class WebDataDownLoader

        Dim tcs As New TaskCompletionSource(Of String())
        Dim nameToSearch As String
        Dim token As CancellationToken
        Dim results As New List(Of String)
        Dim m_lock As Object
        Dim count As Integer
        Dim addresses() As String

        Shared Sub Main()

            Dim downloader As New WebDataDownLoader()
            downloader.addresses = {"http://www.msnbc.com", "http://www.yahoo.com", _
                                         "http://www.nytimes.com", "http://www.washingtonpost.com", _
                                         "http://www.latimes.com", "http://www.newsday.com"}
            Dim cts As New CancellationTokenSource()

            ' Create a UI thread from which to cancel the entire operation
            Task.Factory.StartNew(Sub()
                                      Console.WriteLine("Press c to cancel")
                                      If Console.ReadKey().KeyChar = "c"c Then
                                          cts.Cancel()
                                      End If
                                  End Sub)

            ' Using a neutral search term that is sure to get some hits on English web sites.
            ' Please substitute your favorite search term.
            downloader.nameToSearch = "the"
            Dim webTask As Task(Of String()) = downloader.GetWordCounts(downloader.addresses, downloader.nameToSearch, cts.Token)

            ' Do some other work here unless the method has already completed.
            If (webTask.IsCompleted = False) Then
                ' Simulate some work
                Thread.SpinWait(5000000)
            End If

            Dim results As String() = Nothing
            Try
                results = webTask.Result
            Catch ae As AggregateException
                For Each ex As Exception In ae.InnerExceptions
                    If (TypeOf (ex) Is OperationCanceledException) Then
                        Dim oce As OperationCanceledException = CType(ex, OperationCanceledException)
                        If oce.CancellationToken = cts.Token Then
                            Console.WriteLine("Operation canceled by user.")
                        End If
                    Else
                        Console.WriteLine(ex.Message)
                    End If

                Next
            Finally
                cts.Dispose()
            End Try

            If (Not results Is Nothing) Then
                For Each item As String In results
                    Console.WriteLine(item)
                Next
            End If

            Console.WriteLine("Press any key to exit")
            Console.ReadKey()
        End Sub

        Public Function GetWordCounts(ByVal urls() As String, ByVal str As String, ByVal token As CancellationToken) As Task(Of String())

            Dim webClients() As WebClient
            ReDim webClients(urls.Length)
            m_lock = New Object()

            ' If the user cancels the CancellationToken, then we can use the
            ' WebClient's ability to cancel its own async operations.
            token.Register(Sub()
                               For Each wc As WebClient In webClients
                                   If Not wc Is Nothing Then
                                       wc.CancelAsync()
                                   End If
                               Next
                           End Sub)


            For i As Integer = 0 To urls.Length - 1
                webClients(i) = New WebClient()

                ' Specify the callback for the DownloadStringCompleted
                ' event that will be raised by this WebClient instance.
                AddHandler webClients(i).DownloadStringCompleted, AddressOf WebEventHandler

                Dim address As Uri = Nothing
                Try
                    address = New Uri(urls(i))
                    ' Pass the address, and also use it for the userToken 
                    ' to identify the page when the delegate is invoked.
                    webClients(i).DownloadStringAsync(address, address)
                Catch ex As UriFormatException
                    tcs.TrySetException(ex)
                    Return tcs.Task
                End Try

            Next

            ' Return the underlying Task. The client code
            ' waits on the Result property, and handles exceptions
            ' in the try-catch block there.
            Return tcs.Task
        End Function



        Public Sub WebEventHandler(ByVal sender As Object, ByVal args As DownloadStringCompletedEventArgs)

            If args.Cancelled = True Then
                tcs.TrySetCanceled()
                Return
            ElseIf Not args.Error Is Nothing Then
                tcs.TrySetException(args.Error)
                Return
            Else
                ' Split the string into an array of words,
                ' then count the number of elements that match
                ' the search term.
                Dim words() As String = args.Result.Split(" "c)
                Dim NAME As String = nameToSearch.ToUpper()
                Dim nameCount = (From word In words.AsParallel()
                                 Where word.ToUpper().Contains(NAME)
                                 Select word).Count()

                ' Associate the results with the url, and add new string to the array that 
                ' the underlying Task object will return in its Result property.
                results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, nameToSearch))
            End If

            SyncLock (m_lock)
                count = count + 1
                If (count = addresses.Length) Then
                    tcs.TrySetResult(results.ToArray())
                End If
            End SyncLock
        End Sub
        '</snippet08>
    End Class


    '<snippet09>
    Class Calculator
        Public Function BeginCalculate(ByVal decimalPlaces As Integer, ByVal ac As AsyncCallback, ByVal state As Object) As IAsyncResult
            Console.WriteLine("Calling BeginCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId)
            Dim myTask = Task(Of String).Factory.StartNew(Function(obj) Compute(decimalPlaces), state)
            myTask.ContinueWith(Sub(antecedent) ac(myTask))

        End Function
        Private Function Compute(ByVal decimalPlaces As Integer)
            Console.WriteLine("Calling compute on thread {0}", Thread.CurrentThread.ManagedThreadId)

            ' Simulating some heavy work.
            Thread.SpinWait(500000000)

            ' Actual implementation left as exercise for the reader.
            ' Several examples are available on the Web.
            Return "3.14159265358979323846264338327950288"
        End Function

        Public Function EndCalculate(ByVal ar As IAsyncResult) As String
            Console.WriteLine("Calling EndCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId)
            Return CType(ar, Task(Of String)).Result
        End Function
    End Class

    Class CalculatorClient
        Shared decimalPlaces As Integer
        Shared Sub Main()
            Dim calc As New Calculator
            Dim places As Integer = 35
            Dim callback As New AsyncCallback(AddressOf PrintResult)
            Dim ar As IAsyncResult = calc.BeginCalculate(places, callback, calc)

            ' Do some work on this thread while the calculator is busy.
            Console.WriteLine("Working...")
            Thread.SpinWait(500000)
            Console.ReadLine()
        End Sub

        Public Shared Sub PrintResult(ByVal result As IAsyncResult)
            Dim c As Calculator = CType(result.AsyncState, Calculator)
            Dim piString As String = c.EndCalculate(result)
            Console.WriteLine("Calling PrintResult on thread {0}; result = {1}",
                       Thread.CurrentThread.ManagedThreadId, piString)
        End Sub

    End Class
    '</snippet09>

    Class FileStreamDemo
        Shared Sub Main()
            Dim fsd As New FileStreamDemo()
            fsd.ShowCallingFromAsync()
        End Sub

        '<snippet03>
        Const MAX_FILE_SIZE As Integer = 14000000
        Shared Function GetFileStringAsync(ByVal path As String) As Task(Of String)
            Dim fi As New FileInfo(path)
            Dim data(fi.Length - 1) As Byte

            Dim fs As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, True)

            ' Task(Of Integer) returns the number of bytes read
            Dim myTask As Task(Of Integer) = Task(Of Integer).Factory.FromAsync(
                AddressOf fs.BeginRead, AddressOf fs.EndRead, data, 0, data.Length, Nothing)

            ' It is possible to do other work here while waiting
            ' for the antecedent task to complete.
            ' ...

            ' Add the continuation, which returns a Task<string>. 
            Return myTask.ContinueWith(Function(antecedent)
                                           fs.Close()
                                           If (antecedent.Result < 100) Then
                                               Return "Data is too small to bother with."
                                           End If
                                           ' If we did not receive the entire file, the end of the
                                           ' data buffer will contain garbage.
                                           If (antecedent.Result < data.Length) Then
                                               Array.Resize(data, antecedent.Result)
                                           End If

                                           ' Will be returned in the Result property of the Task<string>
                                           ' at some future point after the asynchronous file I/O operation completes.
                                           Return New UTF8Encoding().GetString(data)
                                       End Function)

        End Function
        '</snippet03>

        Public Sub ShowCallingFromAsync()
            Dim path As String = "\\docbuild2\public\Main\Logs\DllDiffReport\DllDiffReport_BetweenInBuildAndInPublicShare.html"
            '<snippet04>
            Dim myTask As Task(Of String) = GetFileStringAsync(path)

            ' Do some other work
            ' ...

            Try
                Console.WriteLine(myTask.Result.Substring(0, 500))
            Catch ex As AggregateException
                Console.WriteLine(ex.InnerException.Message)
            End Try
            '</snippet04>

            Console.WriteLine("Press Enter to exit.")
            Console.ReadLine()
        End Sub

        '<snippet05>
        Public Function GetFileStringAsync2(ByVal path As String) As Task(Of String)
            Dim fi = New FileInfo(path)
            Dim data(fi.Length - 1) As Byte
            Dim state As New MyCustomState()

            Dim fs As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, True)
            ' We still pass null for the last parameter because
            ' the state variable is visible to the continuation delegate.
            Dim myTask As Task(Of Integer) = Task(Of Integer).Factory.FromAsync(
                    AddressOf fs.BeginRead, AddressOf fs.EndRead, data, 0, data.Length, Nothing)

            Return myTask.ContinueWith(Function(antecedent)
                                           fs.Close()
                                           ' Capture custom state data directly in the user delegate.
                                           ' No need to pass it through the FromAsync method.
                                           If (state.StateData.Contains("New York, New York")) Then
                                               Return "Start spreading the news!"
                                           End If

                                           ' If we did not receive the entire file, the end of the
                                           ' data buffer will contain garbage.
                                           If (antecedent.Result < data.Length) Then
                                               Array.Resize(data, antecedent.Result)
                                           End If
                                           '/ Will be returned in the Result property of the Task<string>
                                           '/ at some future point after the asynchronous file I/O operation completes.
                                           Return New UTF8Encoding().GetString(data)
                                       End Function)

        End Function
        '</snippet05>


        '<snippet06>
        Public Function GetMultiFileData(ByVal filesToRead As String()) As Task(Of String)
            Dim fs As FileStream
            Dim tasks(filesToRead.Length - 1) As Task(Of String)
            Dim fileData() As Byte = Nothing
            For i As Integer = 0 To filesToRead.Length
                fileData(&H1000) = New Byte()
                fs = New FileStream(filesToRead(i), FileMode.Open, FileAccess.Read, FileShare.Read, fileData.Length, True)

                ' By adding the continuation here, the 
                ' Result of each task will be a string.
                tasks(i) = Task(Of Integer).Factory.FromAsync(AddressOf fs.BeginRead,
                                                              AddressOf fs.EndRead,
                                                              fileData,
                                                              0,
                                                              fileData.Length,
                                                              Nothing).
                                                          ContinueWith(Function(antecedent)
                                                                           fs.Close()
                                                                           'If we did not receive the entire file, the end of the
                                                                           ' data buffer will contain garbage.
                                                                           If (antecedent.Result < fileData.Length) Then
                                                                               ReDim Preserve fileData(antecedent.Result)
                                                                           End If

                                                                           'Will be returned in the Result property of the Task<string>
                                                                           ' at some future point after the asynchronous file I/O operation completes.
                                                                           Return New UTF8Encoding().GetString(fileData)
                                                                       End Function)
            Next

            Return Task(Of String).Factory.ContinueWhenAll(tasks, Function(data)

                                                                      ' Propagate all exceptions and mark all faulted tasks as observed.
                                                                      Task.WaitAll(data)

                                                                      ' Combine the results from all tasks.
                                                                      Dim sb As New StringBuilder()
                                                                      For Each t As Task(Of String) In data
                                                                          sb.Append(t.Result)
                                                                      Next
                                                                      ' Final result to be returned eventually on the calling thread.
                                                                      Return sb.ToString()
                                                                  End Function)
        End Function
        '</snippet06>

        Class MyCustomState
            Public StateData As String
            Public Sub New()
                StateData = "Hello"
            End Sub
        End Class


    End Class


End Module

Synchronize multiple FromAsync tasks

The static System.Threading.Tasks.TaskFactory.ContinueWhenAll and System.Threading.Tasks.TaskFactory.ContinueWhenAny methods provide added flexibility when used in conjunction with the FromAsync methods. The following example shows how to initiate multiple asynchronous I/O operations, and then wait for all of them to complete before you execute the continuation.

sample · csharpsamples/snippets/csharp/VS_Snippets_Misc/fromasync/cs/fromasync.cs06

csharp
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net;

namespace specialNamespaceForOneMethodSignature
{
    using System;
    class Task<TResult>
    {
        public TResult TValue;
    }
    class Dummy <TResult>
    {
            // <snippet01>
            public Task<TResult> FromAsync<TArg1, TArg2, TArg3>(
                Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, //BeginRead
                 Func<IAsyncResult, TResult> endMethod, //EndRead
                 TArg1 arg1, // the byte[] buffer
                 TArg2 arg2, // the offset in arg1 at which to start writing data
                 TArg3 arg3, // the maximum number of bytes to read
                 object state // optional state information
                )
                //</snippet01>
        { return new Task<TResult>(); }
    }
}

namespace APM_Task
{
    class TestFileStreamNaive
    {
        //<snippet02>
        public string GetFileData(string path)
        {
            FileInfo fi = new FileInfo(path);
            byte[] data = new byte[fi.Length];
            FileStream fs;

            // a using statement is ok here because we Wait before returning
            // and will be done with the filestream.
            using (fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true))
            {
                //Task<int> returns the number of bytes read
                Task<int> task = Task<int>.Factory.FromAsync(
                        fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

                task.Wait();
                return new UTF8Encoding().GetString(data);
            }
        }
        //</snippet02>
        static void Main()
        {
            TestFileStreamNaive instance = new TestFileStreamNaive();
            string s = instance.GetFileData("testfile.txt");
        }
    }

    class AsyncResult : IAsyncResult
    {

        public AsyncResult() { }
        public object AsyncState
        {
            get { return "Hello from a parallel universe."; }
        }

        public WaitHandle AsyncWaitHandle
        {
            get { throw new NotImplementedException(); }
        }

        public bool CompletedSynchronously
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsCompleted
        {
            get { throw new NotImplementedException(); }
        }

        static IAsyncResult DoSomethingAsynchronously()
        {
            return new AsyncResult();
        }

        //<snippet07>
        static Task<String> ReturnTaskFromAsyncResult()
        {
            IAsyncResult ar = DoSomethingAsynchronously();
            Task<String> t = Task<string>.Factory.FromAsync(ar, _ =>
                {
                    return (string)ar.AsyncState;
                });

            return t;
        }
        //</snippet07>
    }

    //<snippet08>
    class WebDataDownloader
    {

        static void Main()
        {
            WebDataDownloader downloader = new WebDataDownloader();
            string[] addresses = { "http://www.msnbc.com", "http://www.yahoo.com",
                                     "http://www.nytimes.com", "http://www.washingtonpost.com",
                                     "http://www.latimes.com", "http://www.newsday.com" };
            CancellationTokenSource cts = new CancellationTokenSource();

            // Create a UI thread from which to cancel the entire operation
            Task.Factory.StartNew(() =>
            {
                Console.WriteLine("Press c to cancel");
                if (Console.ReadKey().KeyChar == 'c')
                    cts.Cancel();
            });

            // Using a neutral search term that is sure to get some hits.
            Task<string[]> webTask = downloader.GetWordCounts(addresses, "the", cts.Token);

            // Do some other work here unless the method has already completed.
            if (!webTask.IsCompleted)
            {
                // Simulate some work.
                Thread.SpinWait(5000000);
            }

            string[] results = null;
            try
            {
                results = webTask.Result;
            }
            catch (AggregateException e)
            {
                foreach (var ex in e.InnerExceptions)
                {
                    OperationCanceledException oce = ex as OperationCanceledException;
                    if (oce != null)
                    {
                        if (oce.CancellationToken == cts.Token)
                        {
                            Console.WriteLine("Operation canceled by user.");
                        }
                    }
                    else
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            finally
            {
                cts.Dispose();
            }
            if (results != null)
            {
                foreach (var item in results)
                    Console.WriteLine(item);
            }
            Console.ReadKey();
        }

        Task<string[]> GetWordCounts(string[] urls, string name, CancellationToken token)
        {
            TaskCompletionSource<string[]> tcs = new TaskCompletionSource<string[]>();
            WebClient[] webClients = new WebClient[urls.Length];

            // If the user cancels the CancellationToken, then we can use the
            // WebClient's ability to cancel its own async operations.
            token.Register(() =>
                {
                    foreach (var wc in webClients)
                    {
                        if (wc != null)
                            wc.CancelAsync();
                    }
                });

            object m_lock = new object();
            int count = 0;
            List<string> results = new List<string>();
            for (int i = 0; i < urls.Length; i++)
            {
                webClients[i] = new WebClient();

                #region callback
                // Specify the callback for the DownloadStringCompleted
                // event that will be raised by this WebClient instance.
                webClients[i].DownloadStringCompleted += (obj, args) =>
                 {
                     if (args.Cancelled == true)
                     {
                         tcs.TrySetCanceled();
                         return;
                     }
                     else if (args.Error != null)
                     {
                         // Pass through to the underlying Task
                         // any exceptions thrown by the WebClient
                         // during the asynchronous operation.
                         tcs.TrySetException(args.Error);
                         return;
                     }
                     else
                     {
                         // Split the string into an array of words,
                         // then count the number of elements that match
                         // the search term.
                         string[] words = null;
                         words = args.Result.Split(' ');
                         string NAME = name.ToUpper();
                         int nameCount = (from word in words.AsParallel()
                                          where word.ToUpper().Contains(NAME)
                                          select word)
                                         .Count();

                         // Associate the results with the url, and add new string to the array that
                         // the underlying Task object will return in its Result property.
                         results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, name));
                     }

                     // If this is the last async operation to complete,
                     // then set the Result property on the underlying Task.
                     lock (m_lock)
                     {
                         count++;
                         if (count == urls.Length)
                         {
                             tcs.TrySetResult(results.ToArray());
                         }
                     }
                 };
                #endregion

                // Call DownloadStringAsync for each URL.
                Uri address = null;
                try
                {
                    address = new Uri(urls[i]);
                    // Pass the address, and also use it for the userToken
                    // to identify the page when the delegate is invoked.
                    webClients[i].DownloadStringAsync(address, address);
                }

                catch (UriFormatException ex)
                {
                    // Abandon the entire operation if one url is malformed.
                    // Other actions are possible here.
                    tcs.TrySetException(ex);
                    return tcs.Task;
                }
            }

            // Return the underlying Task. The client code
            // waits on the Result property, and handles exceptions
            // in the try-catch block there.
            return tcs.Task;
        }
        //</snippet08>
    }

    #region snippet09
    class Calculator
    {
        public IAsyncResult BeginCalculate(int decimalPlaces, AsyncCallback ac, object state)
        {
            Console.WriteLine($"Calling BeginCalculate on thread {Thread.CurrentThread.ManagedThreadId}");
            Task<string> f = Task<string>.Factory.StartNew(_ => Compute(decimalPlaces), state);
            if (ac != null) f.ContinueWith((res) => ac(f));
            return f;
        }

        public string Compute(int numPlaces)
        {
            Console.WriteLine($"Calling compute on thread {Thread.CurrentThread.ManagedThreadId}");

            // Simulating some heavy work.
            Thread.SpinWait(500000000);

            // Actual implementation left as exercise for the reader.
            // Several examples are available on the Web.
            return "3.14159265358979323846264338327950288";
        }

        public string EndCalculate(IAsyncResult ar)
        {
            Console.WriteLine($"Calling EndCalculate on thread {Thread.CurrentThread.ManagedThreadId}");
            return ((Task<string>)ar).Result;
        }
    }

    public class CalculatorClient
    {
        static int decimalPlaces = 12;
        public static void Main()
        {
            Calculator calc = new Calculator();
            int places = 35;

            AsyncCallback callBack = new AsyncCallback(PrintResult);
            IAsyncResult ar = calc.BeginCalculate(places, callBack, calc);

            // Do some work on this thread while the calculator is busy.
            Console.WriteLine("Working...");
            Thread.SpinWait(500000);
            Console.ReadLine();
        }

        public static void PrintResult(IAsyncResult result)
        {
            Calculator c = (Calculator)result.AsyncState;
            string piString = c.EndCalculate(result);
            Console.WriteLine($"Calling PrintResult on thread {Thread.CurrentThread.ManagedThreadId}; result = {piString}");
        }
    }
    #endregion
} //end APM_Task namespace

class FileStreamDemo
{
    static void Main()
    {
        FileStreamDemo fsd = new FileStreamDemo();
        //  var task = fsd.GetMultiFileData(new string[1]);
         fsd.ShowCallingFromAsync();
       // fsd.ShowCallingFromAsyncFileData();
        //  fsd.ProcessFilesAsync();
        //  task.Wait();
        //  Console.WriteLine(task.Result);

        Console.WriteLine("press any key");
        Console.ReadLine();
    }
    //<snippet03>
    const int MAX_FILE_SIZE = 14000000;
    public static Task<string> GetFileStringAsync(string path)
    {
        FileInfo fi = new FileInfo(path);
        byte[] data = null;
        data = new byte[fi.Length];

        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

        //Task<int> returns the number of bytes read
        Task<int> task = Task<int>.Factory.FromAsync(
                fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

        // It is possible to do other work here while waiting
        // for the antecedent task to complete.
        // ...

        // Add the continuation, which returns a Task<string>.
        return task.ContinueWith((antecedent) =>
        {
            fs.Close();

            // Result = "number of bytes read" (if we need it.)
            if (antecedent.Result < 100)
            {
                return "Data is too small to bother with.";
            }
            else
            {
                // If we did not receive the entire file, the end of the
                // data buffer will contain garbage.
                if (antecedent.Result < data.Length)
                    Array.Resize(ref data, antecedent.Result);

                // Will be returned in the Result property of the Task<string>
                // at some future point after the asynchronous file I/O operation completes.
                return new UTF8Encoding().GetString(data);
            }
        });
    }
    //</snippet03>

        void ShowCallingFromAsync()
        {
            string path = @"\\docbuild2\public\Main\Logs\DllDiffReport\DllDiffReport_BetweenInBuildAndInProduct.html";
            //<snippet04>

            Task<string> t = GetFileStringAsync(path);

            // Do some other work:
            // ...

            try
            {
                 Console.WriteLine(t.Result.Substring(0, 500));
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(ae.InnerException.Message);
            }
            //</snippet04>

            Console.ReadLine();
        }

        //not used???
        //<snippet12>
        public static Task<int> GetFileDataAsync(string path, ref byte[] data)
        {
            // Error handling omitted for brevity...

            FileInfo fi = new FileInfo(path);
            data = new byte[fi.Length];
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

            // Task<int> returns the number of bytes read
            // Pass FileStream in AsyncState parameter in order to close it from the call site.
            return Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, fs);
        }
        //</snippet12>

        void ShowCallingFromAsyncFileData()
        {
            //<snippet13>
            string path = @"\\remoteComputer\filename.txt";

            byte[] buffer = null;
            Task<int> t = GetFileDataAsync(path, ref buffer);

            // Can do some other work here:
            // ...
            for(int i = 0; i < 10; i++)
                Console.Write("Working... ");

            var result =
            t.ContinueWith((antecedent) =>
            {
                // Error handling omitted...

                return new UTF8Encoding().GetString(buffer);
            });

            // Can also do some other work here:
            // ...
            for (int i = 0; i < 10; i++)
                Console.Write("Working more... ");
            try
            {
                t.Wait();
                Console.WriteLine(result.Result.Substring(0, 50));
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(ae.InnerException.Message);
            }
            //</snippet13>

            Console.ReadLine();
        }
        // nested dummy class for snippet0 5 below
        class MyCustomState
        {
            public string StateData { get; set; }
            public MyCustomState() { StateData = "Hello"; }
        }
        private MyCustomState GetCustomState()
        {
            return new MyCustomState();
        }

        //<snippet05>
        public Task<string> GetFileStringAsync2(string path)
        {
            FileInfo fi = new FileInfo(path);
            byte[] data = new byte[fi.Length];
            MyCustomState state = GetCustomState();
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);
            // We still pass null for the last parameter because
            // the state variable is visible to the continuation delegate.
            Task<int> task = Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

            return task.ContinueWith((antecedent) =>
            {
                // It is safe to close the filestream now.
                fs.Close();

                // Capture custom state data directly in the user delegate.
                // No need to pass it through the FromAsync method.
                if (state.StateData.Contains("New York, New York"))
                {
                    return "Start spreading the news!";
                }
                else
                {
                    // If we did not receive the entire file, the end of the
                    // data buffer will contain garbage.
                    if (antecedent.Result < data.Length)
                        Array.Resize(ref data, antecedent.Result);

                    // Will be returned in the Result property of the Task<string>
                    // at some future point after the asynchronous file I/O operation completes.
                    return new UTF8Encoding().GetString(data);
                }
            });
        }
        //</snippet05>

        string[] GetFilesToRead()
        {
            return new string[3] { "test.txt", "test1.txt", "test2.txt" };
        }
        //<snippet06>
        public Task<string> GetMultiFileData(string[] filesToRead)
        {
            FileStream fs;
            Task<string>[] tasks = new Task<string>[filesToRead.Length];
            byte[] fileData = null;
            for (int i = 0; i < filesToRead.Length; i++)
            {
                fileData = new byte[0x1000];
                fs = new FileStream(filesToRead[i], FileMode.Open, FileAccess.Read, FileShare.Read, fileData.Length, true);

                // By adding the continuation here, the
                // Result of each task will be a string.
                tasks[i] = Task<int>.Factory.FromAsync(
                         fs.BeginRead, fs.EndRead, fileData, 0, fileData.Length, null)
                         .ContinueWith((antecedent) =>
                             {
                                 fs.Close();

                                 // If we did not receive the entire file, the end of the
                                 // data buffer will contain garbage.
                                 if (antecedent.Result < fileData.Length)
                                     Array.Resize(ref fileData, antecedent.Result);

                                 // Will be returned in the Result property of the Task<string>
                                 // at some future point after the asynchronous file I/O operation completes.
                                 return new UTF8Encoding().GetString(fileData);
                             });
            }

            // Wait for all tasks to complete.
            return Task<string>.Factory.ContinueWhenAll(tasks, (data) =>
            {
                // Propagate all exceptions and mark all faulted tasks as observed.
                Task.WaitAll(data);

                // Combine the results from all tasks.
                StringBuilder sb = new StringBuilder();
                foreach (var t in data)
                {
                    sb.Append(t.Result);
                }
                // Final result to be returned eventually on the calling thread.
                return sb.ToString();
            });
        }
        //</snippet06>
        public string ProcessData(Task[] arr)
        {
            return "processed";
        }

        //<snippet11>
        void ProcessFilesAsync()
        {
            string[] files = Directory.GetFiles(@"\\remoteComputer\folderName\", "*.*", SearchOption.AllDirectories);
            CancellationTokenSource cts = new CancellationTokenSource();

            List<Task<string>> tasks = new List<Task<string>>();

            foreach (var file in files)
            {
                Task<string> t = null;
                try
                {
                    t = GetFileStringAsync3(file, cts.Token);
                }
                catch
                {
                    Console.WriteLine($"Argument exception on {file}");
                    continue;
                }
                t.ContinueWith((antecedent) => ProcessFileData(antecedent.Result));
                tasks.Add(t);
            }
            Console.WriteLine("All tasks have been added.");
            // Do some work here.

            try
            {
                Task.WaitAll(tasks.ToArray());
                Console.WriteLine("All tasks have completed.");
            }
            catch (AggregateException ae)
            {
                foreach (var ex in ae.InnerExceptions)
                    Console.WriteLine(ex.Message);
            }
            finally
            {
                cts.Dispose();
            }

            // If needed, perform a final step after all tasks have completed.
            Task.Factory.ContinueWhenAll(tasks.ToArray(), (arr) =>
                {
                    foreach (var t in arr)
                    {
                        CompareFileData(t.Result);
                    }
                });
        }

        public Task<string> GetFileStringAsync3(string path, CancellationToken token)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(String.Format("Could not find {0}", path));
            }
            FileInfo fi = new FileInfo(path);
            byte[] data = null;

            if (fi.Length < MAX_FILE_SIZE && fi.Length > 0)
        {
            data = new byte[fi.Length];
        }
        else
            {
                throw new ArgumentException(String.Format("{0} is too big to load!", path));
            }

            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

            // We still pass null for the last parameter because
            // the state variable is visible to the continuation delegate.
            Task<int> task = Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

            return task.ContinueWith((antecedent) =>
            {
                fs.Close();

                // If we did not receive the entire file, the end of the
                // data buffer will contain garbage.
                if (antecedent.Result < data.Length)
                    Array.Resize(ref data, antecedent.Result);

                // Will be returned in the Result property of the Task<string>
                // at some future point after the asynchronous file I/O operation completes.
                return new UTF8Encoding().GetString(data);
            });
        }

        string ProcessFileData(string data)
        {

            string s = data.Substring(0, 80);
            Console.WriteLine(s);
            return s;
        }

        string CompareFileData(string s)
        {
            return "compare results";
        }
        //</snippet11>
    }

sample · vbsamples/snippets/visualbasic/VS_Snippets_Misc/fromasync/vb/module1.vb06

vb
Imports System.Collections.Generic
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.Threading.Tasks

Namespace specialNamespaceForOneMethodSignature
    Class Task(Of TResult)
        Public TValue As TResult
    End Class
    Class Dummy(Of TResult)
        '<snippet01>
        Public Function FromAsync(Of TArg1, TArg2, TArg3)(
                        ByVal beginMethod As Func(Of TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult),
                        ByVal endMethod As Func(Of IAsyncResult, TResult),
                        ByVal dataBuffer As TArg1,
                        ByVal byteOffsetToStartAt As TArg2,
                        ByVal maxBytesToRead As TArg3,
                        ByVal stateInfo As Object)
            '</snippet01>
            Return New Task(Of TResult)
        End Function


    End Class
End Namespace

Module Module1

    Class AsyncResult
        Public Sub New()

        End Sub
        '<snippet07>
        Shared Function ReturnTaskFromAsyncResult() As Task(Of String)
            Dim ar As IAsyncResult = DoSomethingAsynchronously()
            Dim t As Task(Of String) = Task(Of String).Factory.FromAsync(ar, Function(res) CStr(res.AsyncState))
            Return t
        End Function
        '</snippet07>

        Private Shared Function DoSomethingAsynchronously() As IAsyncResult
            Throw New NotImplementedException
        End Function

    End Class
    '<snippet08>
    Class WebDataDownLoader

        Dim tcs As New TaskCompletionSource(Of String())
        Dim nameToSearch As String
        Dim token As CancellationToken
        Dim results As New List(Of String)
        Dim m_lock As Object
        Dim count As Integer
        Dim addresses() As String

        Shared Sub Main()

            Dim downloader As New WebDataDownLoader()
            downloader.addresses = {"http://www.msnbc.com", "http://www.yahoo.com", _
                                         "http://www.nytimes.com", "http://www.washingtonpost.com", _
                                         "http://www.latimes.com", "http://www.newsday.com"}
            Dim cts As New CancellationTokenSource()

            ' Create a UI thread from which to cancel the entire operation
            Task.Factory.StartNew(Sub()
                                      Console.WriteLine("Press c to cancel")
                                      If Console.ReadKey().KeyChar = "c"c Then
                                          cts.Cancel()
                                      End If
                                  End Sub)

            ' Using a neutral search term that is sure to get some hits on English web sites.
            ' Please substitute your favorite search term.
            downloader.nameToSearch = "the"
            Dim webTask As Task(Of String()) = downloader.GetWordCounts(downloader.addresses, downloader.nameToSearch, cts.Token)

            ' Do some other work here unless the method has already completed.
            If (webTask.IsCompleted = False) Then
                ' Simulate some work
                Thread.SpinWait(5000000)
            End If

            Dim results As String() = Nothing
            Try
                results = webTask.Result
            Catch ae As AggregateException
                For Each ex As Exception In ae.InnerExceptions
                    If (TypeOf (ex) Is OperationCanceledException) Then
                        Dim oce As OperationCanceledException = CType(ex, OperationCanceledException)
                        If oce.CancellationToken = cts.Token Then
                            Console.WriteLine("Operation canceled by user.")
                        End If
                    Else
                        Console.WriteLine(ex.Message)
                    End If

                Next
            Finally
                cts.Dispose()
            End Try

            If (Not results Is Nothing) Then
                For Each item As String In results
                    Console.WriteLine(item)
                Next
            End If

            Console.WriteLine("Press any key to exit")
            Console.ReadKey()
        End Sub

        Public Function GetWordCounts(ByVal urls() As String, ByVal str As String, ByVal token As CancellationToken) As Task(Of String())

            Dim webClients() As WebClient
            ReDim webClients(urls.Length)
            m_lock = New Object()

            ' If the user cancels the CancellationToken, then we can use the
            ' WebClient's ability to cancel its own async operations.
            token.Register(Sub()
                               For Each wc As WebClient In webClients
                                   If Not wc Is Nothing Then
                                       wc.CancelAsync()
                                   End If
                               Next
                           End Sub)


            For i As Integer = 0 To urls.Length - 1
                webClients(i) = New WebClient()

                ' Specify the callback for the DownloadStringCompleted
                ' event that will be raised by this WebClient instance.
                AddHandler webClients(i).DownloadStringCompleted, AddressOf WebEventHandler

                Dim address As Uri = Nothing
                Try
                    address = New Uri(urls(i))
                    ' Pass the address, and also use it for the userToken 
                    ' to identify the page when the delegate is invoked.
                    webClients(i).DownloadStringAsync(address, address)
                Catch ex As UriFormatException
                    tcs.TrySetException(ex)
                    Return tcs.Task
                End Try

            Next

            ' Return the underlying Task. The client code
            ' waits on the Result property, and handles exceptions
            ' in the try-catch block there.
            Return tcs.Task
        End Function



        Public Sub WebEventHandler(ByVal sender As Object, ByVal args As DownloadStringCompletedEventArgs)

            If args.Cancelled = True Then
                tcs.TrySetCanceled()
                Return
            ElseIf Not args.Error Is Nothing Then
                tcs.TrySetException(args.Error)
                Return
            Else
                ' Split the string into an array of words,
                ' then count the number of elements that match
                ' the search term.
                Dim words() As String = args.Result.Split(" "c)
                Dim NAME As String = nameToSearch.ToUpper()
                Dim nameCount = (From word In words.AsParallel()
                                 Where word.ToUpper().Contains(NAME)
                                 Select word).Count()

                ' Associate the results with the url, and add new string to the array that 
                ' the underlying Task object will return in its Result property.
                results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, nameToSearch))
            End If

            SyncLock (m_lock)
                count = count + 1
                If (count = addresses.Length) Then
                    tcs.TrySetResult(results.ToArray())
                End If
            End SyncLock
        End Sub
        '</snippet08>
    End Class


    '<snippet09>
    Class Calculator
        Public Function BeginCalculate(ByVal decimalPlaces As Integer, ByVal ac As AsyncCallback, ByVal state As Object) As IAsyncResult
            Console.WriteLine("Calling BeginCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId)
            Dim myTask = Task(Of String).Factory.StartNew(Function(obj) Compute(decimalPlaces), state)
            myTask.ContinueWith(Sub(antecedent) ac(myTask))

        End Function
        Private Function Compute(ByVal decimalPlaces As Integer)
            Console.WriteLine("Calling compute on thread {0}", Thread.CurrentThread.ManagedThreadId)

            ' Simulating some heavy work.
            Thread.SpinWait(500000000)

            ' Actual implementation left as exercise for the reader.
            ' Several examples are available on the Web.
            Return "3.14159265358979323846264338327950288"
        End Function

        Public Function EndCalculate(ByVal ar As IAsyncResult) As String
            Console.WriteLine("Calling EndCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId)
            Return CType(ar, Task(Of String)).Result
        End Function
    End Class

    Class CalculatorClient
        Shared decimalPlaces As Integer
        Shared Sub Main()
            Dim calc As New Calculator
            Dim places As Integer = 35
            Dim callback As New AsyncCallback(AddressOf PrintResult)
            Dim ar As IAsyncResult = calc.BeginCalculate(places, callback, calc)

            ' Do some work on this thread while the calculator is busy.
            Console.WriteLine("Working...")
            Thread.SpinWait(500000)
            Console.ReadLine()
        End Sub

        Public Shared Sub PrintResult(ByVal result As IAsyncResult)
            Dim c As Calculator = CType(result.AsyncState, Calculator)
            Dim piString As String = c.EndCalculate(result)
            Console.WriteLine("Calling PrintResult on thread {0}; result = {1}",
                       Thread.CurrentThread.ManagedThreadId, piString)
        End Sub

    End Class
    '</snippet09>

    Class FileStreamDemo
        Shared Sub Main()
            Dim fsd As New FileStreamDemo()
            fsd.ShowCallingFromAsync()
        End Sub

        '<snippet03>
        Const MAX_FILE_SIZE As Integer = 14000000
        Shared Function GetFileStringAsync(ByVal path As String) As Task(Of String)
            Dim fi As New FileInfo(path)
            Dim data(fi.Length - 1) As Byte

            Dim fs As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, True)

            ' Task(Of Integer) returns the number of bytes read
            Dim myTask As Task(Of Integer) = Task(Of Integer).Factory.FromAsync(
                AddressOf fs.BeginRead, AddressOf fs.EndRead, data, 0, data.Length, Nothing)

            ' It is possible to do other work here while waiting
            ' for the antecedent task to complete.
            ' ...

            ' Add the continuation, which returns a Task<string>. 
            Return myTask.ContinueWith(Function(antecedent)
                                           fs.Close()
                                           If (antecedent.Result < 100) Then
                                               Return "Data is too small to bother with."
                                           End If
                                           ' If we did not receive the entire file, the end of the
                                           ' data buffer will contain garbage.
                                           If (antecedent.Result < data.Length) Then
                                               Array.Resize(data, antecedent.Result)
                                           End If

                                           ' Will be returned in the Result property of the Task<string>
                                           ' at some future point after the asynchronous file I/O operation completes.
                                           Return New UTF8Encoding().GetString(data)
                                       End Function)

        End Function
        '</snippet03>

        Public Sub ShowCallingFromAsync()
            Dim path As String = "\\docbuild2\public\Main\Logs\DllDiffReport\DllDiffReport_BetweenInBuildAndInPublicShare.html"
            '<snippet04>
            Dim myTask As Task(Of String) = GetFileStringAsync(path)

            ' Do some other work
            ' ...

            Try
                Console.WriteLine(myTask.Result.Substring(0, 500))
            Catch ex As AggregateException
                Console.WriteLine(ex.InnerException.Message)
            End Try
            '</snippet04>

            Console.WriteLine("Press Enter to exit.")
            Console.ReadLine()
        End Sub

        '<snippet05>
        Public Function GetFileStringAsync2(ByVal path As String) As Task(Of String)
            Dim fi = New FileInfo(path)
            Dim data(fi.Length - 1) As Byte
            Dim state As New MyCustomState()

            Dim fs As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, True)
            ' We still pass null for the last parameter because
            ' the state variable is visible to the continuation delegate.
            Dim myTask As Task(Of Integer) = Task(Of Integer).Factory.FromAsync(
                    AddressOf fs.BeginRead, AddressOf fs.EndRead, data, 0, data.Length, Nothing)

            Return myTask.ContinueWith(Function(antecedent)
                                           fs.Close()
                                           ' Capture custom state data directly in the user delegate.
                                           ' No need to pass it through the FromAsync method.
                                           If (state.StateData.Contains("New York, New York")) Then
                                               Return "Start spreading the news!"
                                           End If

                                           ' If we did not receive the entire file, the end of the
                                           ' data buffer will contain garbage.
                                           If (antecedent.Result < data.Length) Then
                                               Array.Resize(data, antecedent.Result)
                                           End If
                                           '/ Will be returned in the Result property of the Task<string>
                                           '/ at some future point after the asynchronous file I/O operation completes.
                                           Return New UTF8Encoding().GetString(data)
                                       End Function)

        End Function
        '</snippet05>


        '<snippet06>
        Public Function GetMultiFileData(ByVal filesToRead As String()) As Task(Of String)
            Dim fs As FileStream
            Dim tasks(filesToRead.Length - 1) As Task(Of String)
            Dim fileData() As Byte = Nothing
            For i As Integer = 0 To filesToRead.Length
                fileData(&H1000) = New Byte()
                fs = New FileStream(filesToRead(i), FileMode.Open, FileAccess.Read, FileShare.Read, fileData.Length, True)

                ' By adding the continuation here, the 
                ' Result of each task will be a string.
                tasks(i) = Task(Of Integer).Factory.FromAsync(AddressOf fs.BeginRead,
                                                              AddressOf fs.EndRead,
                                                              fileData,
                                                              0,
                                                              fileData.Length,
                                                              Nothing).
                                                          ContinueWith(Function(antecedent)
                                                                           fs.Close()
                                                                           'If we did not receive the entire file, the end of the
                                                                           ' data buffer will contain garbage.
                                                                           If (antecedent.Result < fileData.Length) Then
                                                                               ReDim Preserve fileData(antecedent.Result)
                                                                           End If

                                                                           'Will be returned in the Result property of the Task<string>
                                                                           ' at some future point after the asynchronous file I/O operation completes.
                                                                           Return New UTF8Encoding().GetString(fileData)
                                                                       End Function)
            Next

            Return Task(Of String).Factory.ContinueWhenAll(tasks, Function(data)

                                                                      ' Propagate all exceptions and mark all faulted tasks as observed.
                                                                      Task.WaitAll(data)

                                                                      ' Combine the results from all tasks.
                                                                      Dim sb As New StringBuilder()
                                                                      For Each t As Task(Of String) In data
                                                                          sb.Append(t.Result)
                                                                      Next
                                                                      ' Final result to be returned eventually on the calling thread.
                                                                      Return sb.ToString()
                                                                  End Function)
        End Function
        '</snippet06>

        Class MyCustomState
            Public StateData As String
            Public Sub New()
                StateData = "Hello"
            End Sub
        End Class


    End Class


End Module

FromAsync tasks for only the End method

For those few cases in which the Begin method requires more than three input parameters or has ref or out parameters, you can use the FromAsync overloads, for example, System.Threading.Tasks.TaskFactory.FromAsync%28System.IAsyncResult%2CSystem.Func%7BSystem.IAsyncResult%2C%7D%29, that represent only the End method. These methods can also be used in any scenario in which you're passed an System.IAsyncResult and want to encapsulate it in a Task.

sample · csharpsamples/snippets/csharp/VS_Snippets_Misc/fromasync/cs/fromasync.cs07

csharp
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net;

namespace specialNamespaceForOneMethodSignature
{
    using System;
    class Task<TResult>
    {
        public TResult TValue;
    }
    class Dummy <TResult>
    {
            // <snippet01>
            public Task<TResult> FromAsync<TArg1, TArg2, TArg3>(
                Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, //BeginRead
                 Func<IAsyncResult, TResult> endMethod, //EndRead
                 TArg1 arg1, // the byte[] buffer
                 TArg2 arg2, // the offset in arg1 at which to start writing data
                 TArg3 arg3, // the maximum number of bytes to read
                 object state // optional state information
                )
                //</snippet01>
        { return new Task<TResult>(); }
    }
}

namespace APM_Task
{
    class TestFileStreamNaive
    {
        //<snippet02>
        public string GetFileData(string path)
        {
            FileInfo fi = new FileInfo(path);
            byte[] data = new byte[fi.Length];
            FileStream fs;

            // a using statement is ok here because we Wait before returning
            // and will be done with the filestream.
            using (fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true))
            {
                //Task<int> returns the number of bytes read
                Task<int> task = Task<int>.Factory.FromAsync(
                        fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

                task.Wait();
                return new UTF8Encoding().GetString(data);
            }
        }
        //</snippet02>
        static void Main()
        {
            TestFileStreamNaive instance = new TestFileStreamNaive();
            string s = instance.GetFileData("testfile.txt");
        }
    }

    class AsyncResult : IAsyncResult
    {

        public AsyncResult() { }
        public object AsyncState
        {
            get { return "Hello from a parallel universe."; }
        }

        public WaitHandle AsyncWaitHandle
        {
            get { throw new NotImplementedException(); }
        }

        public bool CompletedSynchronously
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsCompleted
        {
            get { throw new NotImplementedException(); }
        }

        static IAsyncResult DoSomethingAsynchronously()
        {
            return new AsyncResult();
        }

        //<snippet07>
        static Task<String> ReturnTaskFromAsyncResult()
        {
            IAsyncResult ar = DoSomethingAsynchronously();
            Task<String> t = Task<string>.Factory.FromAsync(ar, _ =>
                {
                    return (string)ar.AsyncState;
                });

            return t;
        }
        //</snippet07>
    }

    //<snippet08>
    class WebDataDownloader
    {

        static void Main()
        {
            WebDataDownloader downloader = new WebDataDownloader();
            string[] addresses = { "http://www.msnbc.com", "http://www.yahoo.com",
                                     "http://www.nytimes.com", "http://www.washingtonpost.com",
                                     "http://www.latimes.com", "http://www.newsday.com" };
            CancellationTokenSource cts = new CancellationTokenSource();

            // Create a UI thread from which to cancel the entire operation
            Task.Factory.StartNew(() =>
            {
                Console.WriteLine("Press c to cancel");
                if (Console.ReadKey().KeyChar == 'c')
                    cts.Cancel();
            });

            // Using a neutral search term that is sure to get some hits.
            Task<string[]> webTask = downloader.GetWordCounts(addresses, "the", cts.Token);

            // Do some other work here unless the method has already completed.
            if (!webTask.IsCompleted)
            {
                // Simulate some work.
                Thread.SpinWait(5000000);
            }

            string[] results = null;
            try
            {
                results = webTask.Result;
            }
            catch (AggregateException e)
            {
                foreach (var ex in e.InnerExceptions)
                {
                    OperationCanceledException oce = ex as OperationCanceledException;
                    if (oce != null)
                    {
                        if (oce.CancellationToken == cts.Token)
                        {
                            Console.WriteLine("Operation canceled by user.");
                        }
                    }
                    else
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            finally
            {
                cts.Dispose();
            }
            if (results != null)
            {
                foreach (var item in results)
                    Console.WriteLine(item);
            }
            Console.ReadKey();
        }

        Task<string[]> GetWordCounts(string[] urls, string name, CancellationToken token)
        {
            TaskCompletionSource<string[]> tcs = new TaskCompletionSource<string[]>();
            WebClient[] webClients = new WebClient[urls.Length];

            // If the user cancels the CancellationToken, then we can use the
            // WebClient's ability to cancel its own async operations.
            token.Register(() =>
                {
                    foreach (var wc in webClients)
                    {
                        if (wc != null)
                            wc.CancelAsync();
                    }
                });

            object m_lock = new object();
            int count = 0;
            List<string> results = new List<string>();
            for (int i = 0; i < urls.Length; i++)
            {
                webClients[i] = new WebClient();

                #region callback
                // Specify the callback for the DownloadStringCompleted
                // event that will be raised by this WebClient instance.
                webClients[i].DownloadStringCompleted += (obj, args) =>
                 {
                     if (args.Cancelled == true)
                     {
                         tcs.TrySetCanceled();
                         return;
                     }
                     else if (args.Error != null)
                     {
                         // Pass through to the underlying Task
                         // any exceptions thrown by the WebClient
                         // during the asynchronous operation.
                         tcs.TrySetException(args.Error);
                         return;
                     }
                     else
                     {
                         // Split the string into an array of words,
                         // then count the number of elements that match
                         // the search term.
                         string[] words = null;
                         words = args.Result.Split(' ');
                         string NAME = name.ToUpper();
                         int nameCount = (from word in words.AsParallel()
                                          where word.ToUpper().Contains(NAME)
                                          select word)
                                         .Count();

                         // Associate the results with the url, and add new string to the array that
                         // the underlying Task object will return in its Result property.
                         results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, name));
                     }

                     // If this is the last async operation to complete,
                     // then set the Result property on the underlying Task.
                     lock (m_lock)
                     {
                         count++;
                         if (count == urls.Length)
                         {
                             tcs.TrySetResult(results.ToArray());
                         }
                     }
                 };
                #endregion

                // Call DownloadStringAsync for each URL.
                Uri address = null;
                try
                {
                    address = new Uri(urls[i]);
                    // Pass the address, and also use it for the userToken
                    // to identify the page when the delegate is invoked.
                    webClients[i].DownloadStringAsync(address, address);
                }

                catch (UriFormatException ex)
                {
                    // Abandon the entire operation if one url is malformed.
                    // Other actions are possible here.
                    tcs.TrySetException(ex);
                    return tcs.Task;
                }
            }

            // Return the underlying Task. The client code
            // waits on the Result property, and handles exceptions
            // in the try-catch block there.
            return tcs.Task;
        }
        //</snippet08>
    }

    #region snippet09
    class Calculator
    {
        public IAsyncResult BeginCalculate(int decimalPlaces, AsyncCallback ac, object state)
        {
            Console.WriteLine($"Calling BeginCalculate on thread {Thread.CurrentThread.ManagedThreadId}");
            Task<string> f = Task<string>.Factory.StartNew(_ => Compute(decimalPlaces), state);
            if (ac != null) f.ContinueWith((res) => ac(f));
            return f;
        }

        public string Compute(int numPlaces)
        {
            Console.WriteLine($"Calling compute on thread {Thread.CurrentThread.ManagedThreadId}");

            // Simulating some heavy work.
            Thread.SpinWait(500000000);

            // Actual implementation left as exercise for the reader.
            // Several examples are available on the Web.
            return "3.14159265358979323846264338327950288";
        }

        public string EndCalculate(IAsyncResult ar)
        {
            Console.WriteLine($"Calling EndCalculate on thread {Thread.CurrentThread.ManagedThreadId}");
            return ((Task<string>)ar).Result;
        }
    }

    public class CalculatorClient
    {
        static int decimalPlaces = 12;
        public static void Main()
        {
            Calculator calc = new Calculator();
            int places = 35;

            AsyncCallback callBack = new AsyncCallback(PrintResult);
            IAsyncResult ar = calc.BeginCalculate(places, callBack, calc);

            // Do some work on this thread while the calculator is busy.
            Console.WriteLine("Working...");
            Thread.SpinWait(500000);
            Console.ReadLine();
        }

        public static void PrintResult(IAsyncResult result)
        {
            Calculator c = (Calculator)result.AsyncState;
            string piString = c.EndCalculate(result);
            Console.WriteLine($"Calling PrintResult on thread {Thread.CurrentThread.ManagedThreadId}; result = {piString}");
        }
    }
    #endregion
} //end APM_Task namespace

class FileStreamDemo
{
    static void Main()
    {
        FileStreamDemo fsd = new FileStreamDemo();
        //  var task = fsd.GetMultiFileData(new string[1]);
         fsd.ShowCallingFromAsync();
       // fsd.ShowCallingFromAsyncFileData();
        //  fsd.ProcessFilesAsync();
        //  task.Wait();
        //  Console.WriteLine(task.Result);

        Console.WriteLine("press any key");
        Console.ReadLine();
    }
    //<snippet03>
    const int MAX_FILE_SIZE = 14000000;
    public static Task<string> GetFileStringAsync(string path)
    {
        FileInfo fi = new FileInfo(path);
        byte[] data = null;
        data = new byte[fi.Length];

        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

        //Task<int> returns the number of bytes read
        Task<int> task = Task<int>.Factory.FromAsync(
                fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

        // It is possible to do other work here while waiting
        // for the antecedent task to complete.
        // ...

        // Add the continuation, which returns a Task<string>.
        return task.ContinueWith((antecedent) =>
        {
            fs.Close();

            // Result = "number of bytes read" (if we need it.)
            if (antecedent.Result < 100)
            {
                return "Data is too small to bother with.";
            }
            else
            {
                // If we did not receive the entire file, the end of the
                // data buffer will contain garbage.
                if (antecedent.Result < data.Length)
                    Array.Resize(ref data, antecedent.Result);

                // Will be returned in the Result property of the Task<string>
                // at some future point after the asynchronous file I/O operation completes.
                return new UTF8Encoding().GetString(data);
            }
        });
    }
    //</snippet03>

        void ShowCallingFromAsync()
        {
            string path = @"\\docbuild2\public\Main\Logs\DllDiffReport\DllDiffReport_BetweenInBuildAndInProduct.html";
            //<snippet04>

            Task<string> t = GetFileStringAsync(path);

            // Do some other work:
            // ...

            try
            {
                 Console.WriteLine(t.Result.Substring(0, 500));
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(ae.InnerException.Message);
            }
            //</snippet04>

            Console.ReadLine();
        }

        //not used???
        //<snippet12>
        public static Task<int> GetFileDataAsync(string path, ref byte[] data)
        {
            // Error handling omitted for brevity...

            FileInfo fi = new FileInfo(path);
            data = new byte[fi.Length];
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

            // Task<int> returns the number of bytes read
            // Pass FileStream in AsyncState parameter in order to close it from the call site.
            return Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, fs);
        }
        //</snippet12>

        void ShowCallingFromAsyncFileData()
        {
            //<snippet13>
            string path = @"\\remoteComputer\filename.txt";

            byte[] buffer = null;
            Task<int> t = GetFileDataAsync(path, ref buffer);

            // Can do some other work here:
            // ...
            for(int i = 0; i < 10; i++)
                Console.Write("Working... ");

            var result =
            t.ContinueWith((antecedent) =>
            {
                // Error handling omitted...

                return new UTF8Encoding().GetString(buffer);
            });

            // Can also do some other work here:
            // ...
            for (int i = 0; i < 10; i++)
                Console.Write("Working more... ");
            try
            {
                t.Wait();
                Console.WriteLine(result.Result.Substring(0, 50));
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(ae.InnerException.Message);
            }
            //</snippet13>

            Console.ReadLine();
        }
        // nested dummy class for snippet0 5 below
        class MyCustomState
        {
            public string StateData { get; set; }
            public MyCustomState() { StateData = "Hello"; }
        }
        private MyCustomState GetCustomState()
        {
            return new MyCustomState();
        }

        //<snippet05>
        public Task<string> GetFileStringAsync2(string path)
        {
            FileInfo fi = new FileInfo(path);
            byte[] data = new byte[fi.Length];
            MyCustomState state = GetCustomState();
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);
            // We still pass null for the last parameter because
            // the state variable is visible to the continuation delegate.
            Task<int> task = Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

            return task.ContinueWith((antecedent) =>
            {
                // It is safe to close the filestream now.
                fs.Close();

                // Capture custom state data directly in the user delegate.
                // No need to pass it through the FromAsync method.
                if (state.StateData.Contains("New York, New York"))
                {
                    return "Start spreading the news!";
                }
                else
                {
                    // If we did not receive the entire file, the end of the
                    // data buffer will contain garbage.
                    if (antecedent.Result < data.Length)
                        Array.Resize(ref data, antecedent.Result);

                    // Will be returned in the Result property of the Task<string>
                    // at some future point after the asynchronous file I/O operation completes.
                    return new UTF8Encoding().GetString(data);
                }
            });
        }
        //</snippet05>

        string[] GetFilesToRead()
        {
            return new string[3] { "test.txt", "test1.txt", "test2.txt" };
        }
        //<snippet06>
        public Task<string> GetMultiFileData(string[] filesToRead)
        {
            FileStream fs;
            Task<string>[] tasks = new Task<string>[filesToRead.Length];
            byte[] fileData = null;
            for (int i = 0; i < filesToRead.Length; i++)
            {
                fileData = new byte[0x1000];
                fs = new FileStream(filesToRead[i], FileMode.Open, FileAccess.Read, FileShare.Read, fileData.Length, true);

                // By adding the continuation here, the
                // Result of each task will be a string.
                tasks[i] = Task<int>.Factory.FromAsync(
                         fs.BeginRead, fs.EndRead, fileData, 0, fileData.Length, null)
                         .ContinueWith((antecedent) =>
                             {
                                 fs.Close();

                                 // If we did not receive the entire file, the end of the
                                 // data buffer will contain garbage.
                                 if (antecedent.Result < fileData.Length)
                                     Array.Resize(ref fileData, antecedent.Result);

                                 // Will be returned in the Result property of the Task<string>
                                 // at some future point after the asynchronous file I/O operation completes.
                                 return new UTF8Encoding().GetString(fileData);
                             });
            }

            // Wait for all tasks to complete.
            return Task<string>.Factory.ContinueWhenAll(tasks, (data) =>
            {
                // Propagate all exceptions and mark all faulted tasks as observed.
                Task.WaitAll(data);

                // Combine the results from all tasks.
                StringBuilder sb = new StringBuilder();
                foreach (var t in data)
                {
                    sb.Append(t.Result);
                }
                // Final result to be returned eventually on the calling thread.
                return sb.ToString();
            });
        }
        //</snippet06>
        public string ProcessData(Task[] arr)
        {
            return "processed";
        }

        //<snippet11>
        void ProcessFilesAsync()
        {
            string[] files = Directory.GetFiles(@"\\remoteComputer\folderName\", "*.*", SearchOption.AllDirectories);
            CancellationTokenSource cts = new CancellationTokenSource();

            List<Task<string>> tasks = new List<Task<string>>();

            foreach (var file in files)
            {
                Task<string> t = null;
                try
                {
                    t = GetFileStringAsync3(file, cts.Token);
                }
                catch
                {
                    Console.WriteLine($"Argument exception on {file}");
                    continue;
                }
                t.ContinueWith((antecedent) => ProcessFileData(antecedent.Result));
                tasks.Add(t);
            }
            Console.WriteLine("All tasks have been added.");
            // Do some work here.

            try
            {
                Task.WaitAll(tasks.ToArray());
                Console.WriteLine("All tasks have completed.");
            }
            catch (AggregateException ae)
            {
                foreach (var ex in ae.InnerExceptions)
                    Console.WriteLine(ex.Message);
            }
            finally
            {
                cts.Dispose();
            }

            // If needed, perform a final step after all tasks have completed.
            Task.Factory.ContinueWhenAll(tasks.ToArray(), (arr) =>
                {
                    foreach (var t in arr)
                    {
                        CompareFileData(t.Result);
                    }
                });
        }

        public Task<string> GetFileStringAsync3(string path, CancellationToken token)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(String.Format("Could not find {0}", path));
            }
            FileInfo fi = new FileInfo(path);
            byte[] data = null;

            if (fi.Length < MAX_FILE_SIZE && fi.Length > 0)
        {
            data = new byte[fi.Length];
        }
        else
            {
                throw new ArgumentException(String.Format("{0} is too big to load!", path));
            }

            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

            // We still pass null for the last parameter because
            // the state variable is visible to the continuation delegate.
            Task<int> task = Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

            return task.ContinueWith((antecedent) =>
            {
                fs.Close();

                // If we did not receive the entire file, the end of the
                // data buffer will contain garbage.
                if (antecedent.Result < data.Length)
                    Array.Resize(ref data, antecedent.Result);

                // Will be returned in the Result property of the Task<string>
                // at some future point after the asynchronous file I/O operation completes.
                return new UTF8Encoding().GetString(data);
            });
        }

        string ProcessFileData(string data)
        {

            string s = data.Substring(0, 80);
            Console.WriteLine(s);
            return s;
        }

        string CompareFileData(string s)
        {
            return "compare results";
        }
        //</snippet11>
    }

sample · vbsamples/snippets/visualbasic/VS_Snippets_Misc/fromasync/vb/module1.vb07

vb
Imports System.Collections.Generic
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.Threading.Tasks

Namespace specialNamespaceForOneMethodSignature
    Class Task(Of TResult)
        Public TValue As TResult
    End Class
    Class Dummy(Of TResult)
        '<snippet01>
        Public Function FromAsync(Of TArg1, TArg2, TArg3)(
                        ByVal beginMethod As Func(Of TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult),
                        ByVal endMethod As Func(Of IAsyncResult, TResult),
                        ByVal dataBuffer As TArg1,
                        ByVal byteOffsetToStartAt As TArg2,
                        ByVal maxBytesToRead As TArg3,
                        ByVal stateInfo As Object)
            '</snippet01>
            Return New Task(Of TResult)
        End Function


    End Class
End Namespace

Module Module1

    Class AsyncResult
        Public Sub New()

        End Sub
        '<snippet07>
        Shared Function ReturnTaskFromAsyncResult() As Task(Of String)
            Dim ar As IAsyncResult = DoSomethingAsynchronously()
            Dim t As Task(Of String) = Task(Of String).Factory.FromAsync(ar, Function(res) CStr(res.AsyncState))
            Return t
        End Function
        '</snippet07>

        Private Shared Function DoSomethingAsynchronously() As IAsyncResult
            Throw New NotImplementedException
        End Function

    End Class
    '<snippet08>
    Class WebDataDownLoader

        Dim tcs As New TaskCompletionSource(Of String())
        Dim nameToSearch As String
        Dim token As CancellationToken
        Dim results As New List(Of String)
        Dim m_lock As Object
        Dim count As Integer
        Dim addresses() As String

        Shared Sub Main()

            Dim downloader As New WebDataDownLoader()
            downloader.addresses = {"http://www.msnbc.com", "http://www.yahoo.com", _
                                         "http://www.nytimes.com", "http://www.washingtonpost.com", _
                                         "http://www.latimes.com", "http://www.newsday.com"}
            Dim cts As New CancellationTokenSource()

            ' Create a UI thread from which to cancel the entire operation
            Task.Factory.StartNew(Sub()
                                      Console.WriteLine("Press c to cancel")
                                      If Console.ReadKey().KeyChar = "c"c Then
                                          cts.Cancel()
                                      End If
                                  End Sub)

            ' Using a neutral search term that is sure to get some hits on English web sites.
            ' Please substitute your favorite search term.
            downloader.nameToSearch = "the"
            Dim webTask As Task(Of String()) = downloader.GetWordCounts(downloader.addresses, downloader.nameToSearch, cts.Token)

            ' Do some other work here unless the method has already completed.
            If (webTask.IsCompleted = False) Then
                ' Simulate some work
                Thread.SpinWait(5000000)
            End If

            Dim results As String() = Nothing
            Try
                results = webTask.Result
            Catch ae As AggregateException
                For Each ex As Exception In ae.InnerExceptions
                    If (TypeOf (ex) Is OperationCanceledException) Then
                        Dim oce As OperationCanceledException = CType(ex, OperationCanceledException)
                        If oce.CancellationToken = cts.Token Then
                            Console.WriteLine("Operation canceled by user.")
                        End If
                    Else
                        Console.WriteLine(ex.Message)
                    End If

                Next
            Finally
                cts.Dispose()
            End Try

            If (Not results Is Nothing) Then
                For Each item As String In results
                    Console.WriteLine(item)
                Next
            End If

            Console.WriteLine("Press any key to exit")
            Console.ReadKey()
        End Sub

        Public Function GetWordCounts(ByVal urls() As String, ByVal str As String, ByVal token As CancellationToken) As Task(Of String())

            Dim webClients() As WebClient
            ReDim webClients(urls.Length)
            m_lock = New Object()

            ' If the user cancels the CancellationToken, then we can use the
            ' WebClient's ability to cancel its own async operations.
            token.Register(Sub()
                               For Each wc As WebClient In webClients
                                   If Not wc Is Nothing Then
                                       wc.CancelAsync()
                                   End If
                               Next
                           End Sub)


            For i As Integer = 0 To urls.Length - 1
                webClients(i) = New WebClient()

                ' Specify the callback for the DownloadStringCompleted
                ' event that will be raised by this WebClient instance.
                AddHandler webClients(i).DownloadStringCompleted, AddressOf WebEventHandler

                Dim address As Uri = Nothing
                Try
                    address = New Uri(urls(i))
                    ' Pass the address, and also use it for the userToken 
                    ' to identify the page when the delegate is invoked.
                    webClients(i).DownloadStringAsync(address, address)
                Catch ex As UriFormatException
                    tcs.TrySetException(ex)
                    Return tcs.Task
                End Try

            Next

            ' Return the underlying Task. The client code
            ' waits on the Result property, and handles exceptions
            ' in the try-catch block there.
            Return tcs.Task
        End Function



        Public Sub WebEventHandler(ByVal sender As Object, ByVal args As DownloadStringCompletedEventArgs)

            If args.Cancelled = True Then
                tcs.TrySetCanceled()
                Return
            ElseIf Not args.Error Is Nothing Then
                tcs.TrySetException(args.Error)
                Return
            Else
                ' Split the string into an array of words,
                ' then count the number of elements that match
                ' the search term.
                Dim words() As String = args.Result.Split(" "c)
                Dim NAME As String = nameToSearch.ToUpper()
                Dim nameCount = (From word In words.AsParallel()
                                 Where word.ToUpper().Contains(NAME)
                                 Select word).Count()

                ' Associate the results with the url, and add new string to the array that 
                ' the underlying Task object will return in its Result property.
                results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, nameToSearch))
            End If

            SyncLock (m_lock)
                count = count + 1
                If (count = addresses.Length) Then
                    tcs.TrySetResult(results.ToArray())
                End If
            End SyncLock
        End Sub
        '</snippet08>
    End Class


    '<snippet09>
    Class Calculator
        Public Function BeginCalculate(ByVal decimalPlaces As Integer, ByVal ac As AsyncCallback, ByVal state As Object) As IAsyncResult
            Console.WriteLine("Calling BeginCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId)
            Dim myTask = Task(Of String).Factory.StartNew(Function(obj) Compute(decimalPlaces), state)
            myTask.ContinueWith(Sub(antecedent) ac(myTask))

        End Function
        Private Function Compute(ByVal decimalPlaces As Integer)
            Console.WriteLine("Calling compute on thread {0}", Thread.CurrentThread.ManagedThreadId)

            ' Simulating some heavy work.
            Thread.SpinWait(500000000)

            ' Actual implementation left as exercise for the reader.
            ' Several examples are available on the Web.
            Return "3.14159265358979323846264338327950288"
        End Function

        Public Function EndCalculate(ByVal ar As IAsyncResult) As String
            Console.WriteLine("Calling EndCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId)
            Return CType(ar, Task(Of String)).Result
        End Function
    End Class

    Class CalculatorClient
        Shared decimalPlaces As Integer
        Shared Sub Main()
            Dim calc As New Calculator
            Dim places As Integer = 35
            Dim callback As New AsyncCallback(AddressOf PrintResult)
            Dim ar As IAsyncResult = calc.BeginCalculate(places, callback, calc)

            ' Do some work on this thread while the calculator is busy.
            Console.WriteLine("Working...")
            Thread.SpinWait(500000)
            Console.ReadLine()
        End Sub

        Public Shared Sub PrintResult(ByVal result As IAsyncResult)
            Dim c As Calculator = CType(result.AsyncState, Calculator)
            Dim piString As String = c.EndCalculate(result)
            Console.WriteLine("Calling PrintResult on thread {0}; result = {1}",
                       Thread.CurrentThread.ManagedThreadId, piString)
        End Sub

    End Class
    '</snippet09>

    Class FileStreamDemo
        Shared Sub Main()
            Dim fsd As New FileStreamDemo()
            fsd.ShowCallingFromAsync()
        End Sub

        '<snippet03>
        Const MAX_FILE_SIZE As Integer = 14000000
        Shared Function GetFileStringAsync(ByVal path As String) As Task(Of String)
            Dim fi As New FileInfo(path)
            Dim data(fi.Length - 1) As Byte

            Dim fs As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, True)

            ' Task(Of Integer) returns the number of bytes read
            Dim myTask As Task(Of Integer) = Task(Of Integer).Factory.FromAsync(
                AddressOf fs.BeginRead, AddressOf fs.EndRead, data, 0, data.Length, Nothing)

            ' It is possible to do other work here while waiting
            ' for the antecedent task to complete.
            ' ...

            ' Add the continuation, which returns a Task<string>. 
            Return myTask.ContinueWith(Function(antecedent)
                                           fs.Close()
                                           If (antecedent.Result < 100) Then
                                               Return "Data is too small to bother with."
                                           End If
                                           ' If we did not receive the entire file, the end of the
                                           ' data buffer will contain garbage.
                                           If (antecedent.Result < data.Length) Then
                                               Array.Resize(data, antecedent.Result)
                                           End If

                                           ' Will be returned in the Result property of the Task<string>
                                           ' at some future point after the asynchronous file I/O operation completes.
                                           Return New UTF8Encoding().GetString(data)
                                       End Function)

        End Function
        '</snippet03>

        Public Sub ShowCallingFromAsync()
            Dim path As String = "\\docbuild2\public\Main\Logs\DllDiffReport\DllDiffReport_BetweenInBuildAndInPublicShare.html"
            '<snippet04>
            Dim myTask As Task(Of String) = GetFileStringAsync(path)

            ' Do some other work
            ' ...

            Try
                Console.WriteLine(myTask.Result.Substring(0, 500))
            Catch ex As AggregateException
                Console.WriteLine(ex.InnerException.Message)
            End Try
            '</snippet04>

            Console.WriteLine("Press Enter to exit.")
            Console.ReadLine()
        End Sub

        '<snippet05>
        Public Function GetFileStringAsync2(ByVal path As String) As Task(Of String)
            Dim fi = New FileInfo(path)
            Dim data(fi.Length - 1) As Byte
            Dim state As New MyCustomState()

            Dim fs As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, True)
            ' We still pass null for the last parameter because
            ' the state variable is visible to the continuation delegate.
            Dim myTask As Task(Of Integer) = Task(Of Integer).Factory.FromAsync(
                    AddressOf fs.BeginRead, AddressOf fs.EndRead, data, 0, data.Length, Nothing)

            Return myTask.ContinueWith(Function(antecedent)
                                           fs.Close()
                                           ' Capture custom state data directly in the user delegate.
                                           ' No need to pass it through the FromAsync method.
                                           If (state.StateData.Contains("New York, New York")) Then
                                               Return "Start spreading the news!"
                                           End If

                                           ' If we did not receive the entire file, the end of the
                                           ' data buffer will contain garbage.
                                           If (antecedent.Result < data.Length) Then
                                               Array.Resize(data, antecedent.Result)
                                           End If
                                           '/ Will be returned in the Result property of the Task<string>
                                           '/ at some future point after the asynchronous file I/O operation completes.
                                           Return New UTF8Encoding().GetString(data)
                                       End Function)

        End Function
        '</snippet05>


        '<snippet06>
        Public Function GetMultiFileData(ByVal filesToRead As String()) As Task(Of String)
            Dim fs As FileStream
            Dim tasks(filesToRead.Length - 1) As Task(Of String)
            Dim fileData() As Byte = Nothing
            For i As Integer = 0 To filesToRead.Length
                fileData(&H1000) = New Byte()
                fs = New FileStream(filesToRead(i), FileMode.Open, FileAccess.Read, FileShare.Read, fileData.Length, True)

                ' By adding the continuation here, the 
                ' Result of each task will be a string.
                tasks(i) = Task(Of Integer).Factory.FromAsync(AddressOf fs.BeginRead,
                                                              AddressOf fs.EndRead,
                                                              fileData,
                                                              0,
                                                              fileData.Length,
                                                              Nothing).
                                                          ContinueWith(Function(antecedent)
                                                                           fs.Close()
                                                                           'If we did not receive the entire file, the end of the
                                                                           ' data buffer will contain garbage.
                                                                           If (antecedent.Result < fileData.Length) Then
                                                                               ReDim Preserve fileData(antecedent.Result)
                                                                           End If

                                                                           'Will be returned in the Result property of the Task<string>
                                                                           ' at some future point after the asynchronous file I/O operation completes.
                                                                           Return New UTF8Encoding().GetString(fileData)
                                                                       End Function)
            Next

            Return Task(Of String).Factory.ContinueWhenAll(tasks, Function(data)

                                                                      ' Propagate all exceptions and mark all faulted tasks as observed.
                                                                      Task.WaitAll(data)

                                                                      ' Combine the results from all tasks.
                                                                      Dim sb As New StringBuilder()
                                                                      For Each t As Task(Of String) In data
                                                                          sb.Append(t.Result)
                                                                      Next
                                                                      ' Final result to be returned eventually on the calling thread.
                                                                      Return sb.ToString()
                                                                  End Function)
        End Function
        '</snippet06>

        Class MyCustomState
            Public StateData As String
            Public Sub New()
                StateData = "Hello"
            End Sub
        End Class


    End Class


End Module

Start and cancel FromAsync tasks

The task returned by a FromAsync method has a status of WaitingForActivation and will be started by the system at some point after the task is created. If you attempt to call Start on such a task, an exception will be raised.

You cannot cancel a FromAsync task, because the underlying .NET APIs currently do not support in-progress cancellation of file or network I/O. You can add cancellation functionality to a method that encapsulates a FromAsync call, but you can only respond to the cancellation before FromAsync is called or after it completed (for example, in a continuation task).

Some classes that support EAP, for example, System.Net.WebClient, do support cancellation, and you can integrate that native cancellation functionality by using cancellation tokens.

Expose complex EAP operations As tasks

The TPL does not provide any methods that are specifically designed to encapsulate an event-based asynchronous operation in the same way that the FromAsync family of methods wrap the System.IAsyncResult pattern. However, the TPL does provide the System.Threading.Tasks.TaskCompletionSource class, which can be used to represent any arbitrary set of operations as a System.Threading.Tasks.Task. The operations may be synchronous or asynchronous, and may be I/O bound or compute-bound, or both.

The following example shows how to use a System.Threading.Tasks.TaskCompletionSource to expose a set of asynchronous System.Net.WebClient operations to client code as a basic System.Threading.Tasks.Task. The method lets you enter an array of Web URLs, and a term or name to search for, and then returns the number of times the search term occurs on each site.

sample · csharpsamples/snippets/csharp/VS_Snippets_Misc/fromasync/cs/snippet10.cs10

csharp
// <Snippet10>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

public class SimpleWebExample
{
  public Task<string[]> GetWordCountsSimplified(string[] urls, string name,
                                                CancellationToken token)
  {
      TaskCompletionSource<string[]> tcs = new TaskCompletionSource<string[]>();
      WebClient[] webClients = new WebClient[urls.Length];
      object m_lock = new object();
      int count = 0;
      List<string> results = new List<string>();

      // If the user cancels the CancellationToken, then we can use the
      // WebClient's ability to cancel its own async operations.
      token.Register(() =>
      {
          foreach (var wc in webClients)
          {
              if (wc != null)
                  wc.CancelAsync();
          }
      });

      for (int i = 0; i < urls.Length; i++)
      {
          webClients[i] = new WebClient();

          #region callback
          // Specify the callback for the DownloadStringCompleted
          // event that will be raised by this WebClient instance.
          webClients[i].DownloadStringCompleted += (obj, args) =>
          {

              // Argument validation and exception handling omitted for brevity.

              // Split the string into an array of words,
              // then count the number of elements that match
              // the search term.
              string[] words = args.Result.Split(' ');
              string NAME = name.ToUpper();
              int nameCount = (from word in words.AsParallel()
                               where word.ToUpper().Contains(NAME)
                               select word)
                              .Count();

              // Associate the results with the url, and add new string to the array that
              // the underlying Task object will return in its Result property.
              lock (m_lock)
              {
                 results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, name));

                 // If this is the last async operation to complete,
                 // then set the Result property on the underlying Task.
                 count++;
                 if (count == urls.Length)
                 {
                    tcs.TrySetResult(results.ToArray());
                 }
              }
          };
          #endregion

          // Call DownloadStringAsync for each URL.
          Uri address = null;
          address = new Uri(urls[i]);
          webClients[i].DownloadStringAsync(address, address);
      } // end for

      // Return the underlying Task. The client code
      // waits on the Result property, and handles exceptions
      // in the try-catch block there.
      return tcs.Task;
   }
}
// </Snippet10>

public class Example
{
   public static void Main()
   {
      string[] urls = {"http://www.microsoft.com",
                       "http://www.google.com",
                       "http://www.amazon.com",
                       "http://www.adobe.com" };
      string searchTerm = "investor";
      var ex = new SimpleWebExample();
      var t = ex.GetWordCountsSimplified(urls, searchTerm, CancellationToken.None);
      t.Wait();
      foreach (var value in t.Result)
         Console.WriteLine(value);

      Console.ReadLine();
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_Misc/fromasync/vb/snippet10.vb10

vb
' Visual Basic .NET Document
Option Strict On

' <Snippet10>
Imports System.Collections.Generic
Imports System.Net
Imports System.Threading
Imports System.Threading.Tasks

Public Class SimpleWebExample
    Dim tcs As New TaskCompletionSource(Of String())
    Dim token As CancellationToken
    Dim results As New List(Of String)
    Dim m_lock As New Object()
    Dim count As Integer
    Dim addresses() As String
    Dim nameToSearch As String

    Public Function GetWordCountsSimplified(ByVal urls() As String, ByVal str As String,
                                            ByVal token As CancellationToken) As Task(Of String())
        addresses = urls
        nameToSearch = str

        Dim webClients(urls.Length - 1) As WebClient

        ' If the user cancels the CancellationToken, then we can use the
        ' WebClient's ability to cancel its own async operations.
        token.Register(Sub()
                           For Each wc As WebClient In webClients
                               If wc IsNot Nothing Then
                                   wc.CancelAsync()
                               End If
                           Next
                       End Sub)

        For i As Integer = 0 To urls.Length - 1
            webClients(i) = New WebClient()

            ' Specify the callback for the DownloadStringCompleted
            ' event that will be raised by this WebClient instance.
            AddHandler webClients(i).DownloadStringCompleted, AddressOf WebEventHandler

            Dim address As New Uri(urls(i))
            ' Pass the address, and also use it for the userToken
            ' to identify the page when the delegate is invoked.
            webClients(i).DownloadStringAsync(address, address)
        Next

        ' Return the underlying Task. The client code
        ' waits on the Result property, and handles exceptions
        ' in the try-catch block there.
        Return tcs.Task
    End Function

    Public Sub WebEventHandler(ByVal sender As Object, ByVal args As DownloadStringCompletedEventArgs)

        If args.Cancelled = True Then
            tcs.TrySetCanceled()
            Return
        ElseIf args.Error IsNot Nothing Then
            tcs.TrySetException(args.Error)
            Return
        Else
            ' Split the string into an array of words,
            ' then count the number of elements that match
            ' the search term.
            Dim words() As String = args.Result.Split(" "c)

            Dim name As String = nameToSearch.ToUpper()
            Dim nameCount = (From word In words.AsParallel()
                             Where word.ToUpper().Contains(name)
                             Select word).Count()

            ' Associate the results with the url, and add new string to the array that
            ' the underlying Task object will return in its Result property.
            SyncLock (m_lock)
                results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, nameToSearch))
                count = count + 1
                If (count = addresses.Length) Then
                    tcs.TrySetResult(results.ToArray())
                End If
            End SyncLock
        End If
    End Sub
End Class
' </snippet10>

Public Module Example
    Public Sub Main
        Dim urls() As String = {"http:\\www.microsoft.com",
                                 "http:\\www.google.com",
                                 "http:\\www.amazon.com",
                                 "http:\\www.adobe.com"}
        Dim searchTerm As String = "investor"
        Dim ex As New SimpleWebExample()
        Dim t = ex.GetWordCountsSimplified(urls, searchTerm, Nothing)
        t.Wait()
        For Each value In t.Result
            Console.WriteLine(value)
        Next

        Console.ReadLine()
    End Sub
End Module

For a more complete example, which includes additional exception handling and shows how to call the method from client code, see How to: Wrap EAP Patterns in a Task.

Remember that any task that's created by a System.Threading.Tasks.TaskCompletionSource will be started by that TaskCompletionSource and, therefore, user code should not call the Start method on that task.

Implement the APM pattern by using tasks

In some scenarios, it may be desirable to directly expose the System.IAsyncResult pattern by using begin/end method pairs in an API. For example, you may want to maintain consistency with existing APIs, or you may have automated tools that require this pattern. In such cases, you can use Task objects to simplify how the APM pattern is implemented internally.

The following example shows how to use tasks to implement an APM begin/end method pair for a long-running compute-bound method.

sample · csharpsamples/snippets/csharp/VS_Snippets_Misc/fromasync/cs/fromasync.cs09

csharp
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net;

namespace specialNamespaceForOneMethodSignature
{
    using System;
    class Task<TResult>
    {
        public TResult TValue;
    }
    class Dummy <TResult>
    {
            // <snippet01>
            public Task<TResult> FromAsync<TArg1, TArg2, TArg3>(
                Func<TArg1, TArg2, TArg3, AsyncCallback, object, IAsyncResult> beginMethod, //BeginRead
                 Func<IAsyncResult, TResult> endMethod, //EndRead
                 TArg1 arg1, // the byte[] buffer
                 TArg2 arg2, // the offset in arg1 at which to start writing data
                 TArg3 arg3, // the maximum number of bytes to read
                 object state // optional state information
                )
                //</snippet01>
        { return new Task<TResult>(); }
    }
}

namespace APM_Task
{
    class TestFileStreamNaive
    {
        //<snippet02>
        public string GetFileData(string path)
        {
            FileInfo fi = new FileInfo(path);
            byte[] data = new byte[fi.Length];
            FileStream fs;

            // a using statement is ok here because we Wait before returning
            // and will be done with the filestream.
            using (fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true))
            {
                //Task<int> returns the number of bytes read
                Task<int> task = Task<int>.Factory.FromAsync(
                        fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

                task.Wait();
                return new UTF8Encoding().GetString(data);
            }
        }
        //</snippet02>
        static void Main()
        {
            TestFileStreamNaive instance = new TestFileStreamNaive();
            string s = instance.GetFileData("testfile.txt");
        }
    }

    class AsyncResult : IAsyncResult
    {

        public AsyncResult() { }
        public object AsyncState
        {
            get { return "Hello from a parallel universe."; }
        }

        public WaitHandle AsyncWaitHandle
        {
            get { throw new NotImplementedException(); }
        }

        public bool CompletedSynchronously
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsCompleted
        {
            get { throw new NotImplementedException(); }
        }

        static IAsyncResult DoSomethingAsynchronously()
        {
            return new AsyncResult();
        }

        //<snippet07>
        static Task<String> ReturnTaskFromAsyncResult()
        {
            IAsyncResult ar = DoSomethingAsynchronously();
            Task<String> t = Task<string>.Factory.FromAsync(ar, _ =>
                {
                    return (string)ar.AsyncState;
                });

            return t;
        }
        //</snippet07>
    }

    //<snippet08>
    class WebDataDownloader
    {

        static void Main()
        {
            WebDataDownloader downloader = new WebDataDownloader();
            string[] addresses = { "http://www.msnbc.com", "http://www.yahoo.com",
                                     "http://www.nytimes.com", "http://www.washingtonpost.com",
                                     "http://www.latimes.com", "http://www.newsday.com" };
            CancellationTokenSource cts = new CancellationTokenSource();

            // Create a UI thread from which to cancel the entire operation
            Task.Factory.StartNew(() =>
            {
                Console.WriteLine("Press c to cancel");
                if (Console.ReadKey().KeyChar == 'c')
                    cts.Cancel();
            });

            // Using a neutral search term that is sure to get some hits.
            Task<string[]> webTask = downloader.GetWordCounts(addresses, "the", cts.Token);

            // Do some other work here unless the method has already completed.
            if (!webTask.IsCompleted)
            {
                // Simulate some work.
                Thread.SpinWait(5000000);
            }

            string[] results = null;
            try
            {
                results = webTask.Result;
            }
            catch (AggregateException e)
            {
                foreach (var ex in e.InnerExceptions)
                {
                    OperationCanceledException oce = ex as OperationCanceledException;
                    if (oce != null)
                    {
                        if (oce.CancellationToken == cts.Token)
                        {
                            Console.WriteLine("Operation canceled by user.");
                        }
                    }
                    else
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            finally
            {
                cts.Dispose();
            }
            if (results != null)
            {
                foreach (var item in results)
                    Console.WriteLine(item);
            }
            Console.ReadKey();
        }

        Task<string[]> GetWordCounts(string[] urls, string name, CancellationToken token)
        {
            TaskCompletionSource<string[]> tcs = new TaskCompletionSource<string[]>();
            WebClient[] webClients = new WebClient[urls.Length];

            // If the user cancels the CancellationToken, then we can use the
            // WebClient's ability to cancel its own async operations.
            token.Register(() =>
                {
                    foreach (var wc in webClients)
                    {
                        if (wc != null)
                            wc.CancelAsync();
                    }
                });

            object m_lock = new object();
            int count = 0;
            List<string> results = new List<string>();
            for (int i = 0; i < urls.Length; i++)
            {
                webClients[i] = new WebClient();

                #region callback
                // Specify the callback for the DownloadStringCompleted
                // event that will be raised by this WebClient instance.
                webClients[i].DownloadStringCompleted += (obj, args) =>
                 {
                     if (args.Cancelled == true)
                     {
                         tcs.TrySetCanceled();
                         return;
                     }
                     else if (args.Error != null)
                     {
                         // Pass through to the underlying Task
                         // any exceptions thrown by the WebClient
                         // during the asynchronous operation.
                         tcs.TrySetException(args.Error);
                         return;
                     }
                     else
                     {
                         // Split the string into an array of words,
                         // then count the number of elements that match
                         // the search term.
                         string[] words = null;
                         words = args.Result.Split(' ');
                         string NAME = name.ToUpper();
                         int nameCount = (from word in words.AsParallel()
                                          where word.ToUpper().Contains(NAME)
                                          select word)
                                         .Count();

                         // Associate the results with the url, and add new string to the array that
                         // the underlying Task object will return in its Result property.
                         results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, name));
                     }

                     // If this is the last async operation to complete,
                     // then set the Result property on the underlying Task.
                     lock (m_lock)
                     {
                         count++;
                         if (count == urls.Length)
                         {
                             tcs.TrySetResult(results.ToArray());
                         }
                     }
                 };
                #endregion

                // Call DownloadStringAsync for each URL.
                Uri address = null;
                try
                {
                    address = new Uri(urls[i]);
                    // Pass the address, and also use it for the userToken
                    // to identify the page when the delegate is invoked.
                    webClients[i].DownloadStringAsync(address, address);
                }

                catch (UriFormatException ex)
                {
                    // Abandon the entire operation if one url is malformed.
                    // Other actions are possible here.
                    tcs.TrySetException(ex);
                    return tcs.Task;
                }
            }

            // Return the underlying Task. The client code
            // waits on the Result property, and handles exceptions
            // in the try-catch block there.
            return tcs.Task;
        }
        //</snippet08>
    }

    #region snippet09
    class Calculator
    {
        public IAsyncResult BeginCalculate(int decimalPlaces, AsyncCallback ac, object state)
        {
            Console.WriteLine($"Calling BeginCalculate on thread {Thread.CurrentThread.ManagedThreadId}");
            Task<string> f = Task<string>.Factory.StartNew(_ => Compute(decimalPlaces), state);
            if (ac != null) f.ContinueWith((res) => ac(f));
            return f;
        }

        public string Compute(int numPlaces)
        {
            Console.WriteLine($"Calling compute on thread {Thread.CurrentThread.ManagedThreadId}");

            // Simulating some heavy work.
            Thread.SpinWait(500000000);

            // Actual implementation left as exercise for the reader.
            // Several examples are available on the Web.
            return "3.14159265358979323846264338327950288";
        }

        public string EndCalculate(IAsyncResult ar)
        {
            Console.WriteLine($"Calling EndCalculate on thread {Thread.CurrentThread.ManagedThreadId}");
            return ((Task<string>)ar).Result;
        }
    }

    public class CalculatorClient
    {
        static int decimalPlaces = 12;
        public static void Main()
        {
            Calculator calc = new Calculator();
            int places = 35;

            AsyncCallback callBack = new AsyncCallback(PrintResult);
            IAsyncResult ar = calc.BeginCalculate(places, callBack, calc);

            // Do some work on this thread while the calculator is busy.
            Console.WriteLine("Working...");
            Thread.SpinWait(500000);
            Console.ReadLine();
        }

        public static void PrintResult(IAsyncResult result)
        {
            Calculator c = (Calculator)result.AsyncState;
            string piString = c.EndCalculate(result);
            Console.WriteLine($"Calling PrintResult on thread {Thread.CurrentThread.ManagedThreadId}; result = {piString}");
        }
    }
    #endregion
} //end APM_Task namespace

class FileStreamDemo
{
    static void Main()
    {
        FileStreamDemo fsd = new FileStreamDemo();
        //  var task = fsd.GetMultiFileData(new string[1]);
         fsd.ShowCallingFromAsync();
       // fsd.ShowCallingFromAsyncFileData();
        //  fsd.ProcessFilesAsync();
        //  task.Wait();
        //  Console.WriteLine(task.Result);

        Console.WriteLine("press any key");
        Console.ReadLine();
    }
    //<snippet03>
    const int MAX_FILE_SIZE = 14000000;
    public static Task<string> GetFileStringAsync(string path)
    {
        FileInfo fi = new FileInfo(path);
        byte[] data = null;
        data = new byte[fi.Length];

        FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

        //Task<int> returns the number of bytes read
        Task<int> task = Task<int>.Factory.FromAsync(
                fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

        // It is possible to do other work here while waiting
        // for the antecedent task to complete.
        // ...

        // Add the continuation, which returns a Task<string>.
        return task.ContinueWith((antecedent) =>
        {
            fs.Close();

            // Result = "number of bytes read" (if we need it.)
            if (antecedent.Result < 100)
            {
                return "Data is too small to bother with.";
            }
            else
            {
                // If we did not receive the entire file, the end of the
                // data buffer will contain garbage.
                if (antecedent.Result < data.Length)
                    Array.Resize(ref data, antecedent.Result);

                // Will be returned in the Result property of the Task<string>
                // at some future point after the asynchronous file I/O operation completes.
                return new UTF8Encoding().GetString(data);
            }
        });
    }
    //</snippet03>

        void ShowCallingFromAsync()
        {
            string path = @"\\docbuild2\public\Main\Logs\DllDiffReport\DllDiffReport_BetweenInBuildAndInProduct.html";
            //<snippet04>

            Task<string> t = GetFileStringAsync(path);

            // Do some other work:
            // ...

            try
            {
                 Console.WriteLine(t.Result.Substring(0, 500));
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(ae.InnerException.Message);
            }
            //</snippet04>

            Console.ReadLine();
        }

        //not used???
        //<snippet12>
        public static Task<int> GetFileDataAsync(string path, ref byte[] data)
        {
            // Error handling omitted for brevity...

            FileInfo fi = new FileInfo(path);
            data = new byte[fi.Length];
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

            // Task<int> returns the number of bytes read
            // Pass FileStream in AsyncState parameter in order to close it from the call site.
            return Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, fs);
        }
        //</snippet12>

        void ShowCallingFromAsyncFileData()
        {
            //<snippet13>
            string path = @"\\remoteComputer\filename.txt";

            byte[] buffer = null;
            Task<int> t = GetFileDataAsync(path, ref buffer);

            // Can do some other work here:
            // ...
            for(int i = 0; i < 10; i++)
                Console.Write("Working... ");

            var result =
            t.ContinueWith((antecedent) =>
            {
                // Error handling omitted...

                return new UTF8Encoding().GetString(buffer);
            });

            // Can also do some other work here:
            // ...
            for (int i = 0; i < 10; i++)
                Console.Write("Working more... ");
            try
            {
                t.Wait();
                Console.WriteLine(result.Result.Substring(0, 50));
            }
            catch (AggregateException ae)
            {
                Console.WriteLine(ae.InnerException.Message);
            }
            //</snippet13>

            Console.ReadLine();
        }
        // nested dummy class for snippet0 5 below
        class MyCustomState
        {
            public string StateData { get; set; }
            public MyCustomState() { StateData = "Hello"; }
        }
        private MyCustomState GetCustomState()
        {
            return new MyCustomState();
        }

        //<snippet05>
        public Task<string> GetFileStringAsync2(string path)
        {
            FileInfo fi = new FileInfo(path);
            byte[] data = new byte[fi.Length];
            MyCustomState state = GetCustomState();
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);
            // We still pass null for the last parameter because
            // the state variable is visible to the continuation delegate.
            Task<int> task = Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

            return task.ContinueWith((antecedent) =>
            {
                // It is safe to close the filestream now.
                fs.Close();

                // Capture custom state data directly in the user delegate.
                // No need to pass it through the FromAsync method.
                if (state.StateData.Contains("New York, New York"))
                {
                    return "Start spreading the news!";
                }
                else
                {
                    // If we did not receive the entire file, the end of the
                    // data buffer will contain garbage.
                    if (antecedent.Result < data.Length)
                        Array.Resize(ref data, antecedent.Result);

                    // Will be returned in the Result property of the Task<string>
                    // at some future point after the asynchronous file I/O operation completes.
                    return new UTF8Encoding().GetString(data);
                }
            });
        }
        //</snippet05>

        string[] GetFilesToRead()
        {
            return new string[3] { "test.txt", "test1.txt", "test2.txt" };
        }
        //<snippet06>
        public Task<string> GetMultiFileData(string[] filesToRead)
        {
            FileStream fs;
            Task<string>[] tasks = new Task<string>[filesToRead.Length];
            byte[] fileData = null;
            for (int i = 0; i < filesToRead.Length; i++)
            {
                fileData = new byte[0x1000];
                fs = new FileStream(filesToRead[i], FileMode.Open, FileAccess.Read, FileShare.Read, fileData.Length, true);

                // By adding the continuation here, the
                // Result of each task will be a string.
                tasks[i] = Task<int>.Factory.FromAsync(
                         fs.BeginRead, fs.EndRead, fileData, 0, fileData.Length, null)
                         .ContinueWith((antecedent) =>
                             {
                                 fs.Close();

                                 // If we did not receive the entire file, the end of the
                                 // data buffer will contain garbage.
                                 if (antecedent.Result < fileData.Length)
                                     Array.Resize(ref fileData, antecedent.Result);

                                 // Will be returned in the Result property of the Task<string>
                                 // at some future point after the asynchronous file I/O operation completes.
                                 return new UTF8Encoding().GetString(fileData);
                             });
            }

            // Wait for all tasks to complete.
            return Task<string>.Factory.ContinueWhenAll(tasks, (data) =>
            {
                // Propagate all exceptions and mark all faulted tasks as observed.
                Task.WaitAll(data);

                // Combine the results from all tasks.
                StringBuilder sb = new StringBuilder();
                foreach (var t in data)
                {
                    sb.Append(t.Result);
                }
                // Final result to be returned eventually on the calling thread.
                return sb.ToString();
            });
        }
        //</snippet06>
        public string ProcessData(Task[] arr)
        {
            return "processed";
        }

        //<snippet11>
        void ProcessFilesAsync()
        {
            string[] files = Directory.GetFiles(@"\\remoteComputer\folderName\", "*.*", SearchOption.AllDirectories);
            CancellationTokenSource cts = new CancellationTokenSource();

            List<Task<string>> tasks = new List<Task<string>>();

            foreach (var file in files)
            {
                Task<string> t = null;
                try
                {
                    t = GetFileStringAsync3(file, cts.Token);
                }
                catch
                {
                    Console.WriteLine($"Argument exception on {file}");
                    continue;
                }
                t.ContinueWith((antecedent) => ProcessFileData(antecedent.Result));
                tasks.Add(t);
            }
            Console.WriteLine("All tasks have been added.");
            // Do some work here.

            try
            {
                Task.WaitAll(tasks.ToArray());
                Console.WriteLine("All tasks have completed.");
            }
            catch (AggregateException ae)
            {
                foreach (var ex in ae.InnerExceptions)
                    Console.WriteLine(ex.Message);
            }
            finally
            {
                cts.Dispose();
            }

            // If needed, perform a final step after all tasks have completed.
            Task.Factory.ContinueWhenAll(tasks.ToArray(), (arr) =>
                {
                    foreach (var t in arr)
                    {
                        CompareFileData(t.Result);
                    }
                });
        }

        public Task<string> GetFileStringAsync3(string path, CancellationToken token)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(String.Format("Could not find {0}", path));
            }
            FileInfo fi = new FileInfo(path);
            byte[] data = null;

            if (fi.Length < MAX_FILE_SIZE && fi.Length > 0)
        {
            data = new byte[fi.Length];
        }
        else
            {
                throw new ArgumentException(String.Format("{0} is too big to load!", path));
            }

            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, true);

            // We still pass null for the last parameter because
            // the state variable is visible to the continuation delegate.
            Task<int> task = Task<int>.Factory.FromAsync(
                    fs.BeginRead, fs.EndRead, data, 0, data.Length, null);

            return task.ContinueWith((antecedent) =>
            {
                fs.Close();

                // If we did not receive the entire file, the end of the
                // data buffer will contain garbage.
                if (antecedent.Result < data.Length)
                    Array.Resize(ref data, antecedent.Result);

                // Will be returned in the Result property of the Task<string>
                // at some future point after the asynchronous file I/O operation completes.
                return new UTF8Encoding().GetString(data);
            });
        }

        string ProcessFileData(string data)
        {

            string s = data.Substring(0, 80);
            Console.WriteLine(s);
            return s;
        }

        string CompareFileData(string s)
        {
            return "compare results";
        }
        //</snippet11>
    }

sample · vbsamples/snippets/visualbasic/VS_Snippets_Misc/fromasync/vb/module1.vb09

vb
Imports System.Collections.Generic
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.Threading.Tasks

Namespace specialNamespaceForOneMethodSignature
    Class Task(Of TResult)
        Public TValue As TResult
    End Class
    Class Dummy(Of TResult)
        '<snippet01>
        Public Function FromAsync(Of TArg1, TArg2, TArg3)(
                        ByVal beginMethod As Func(Of TArg1, TArg2, TArg3, AsyncCallback, Object, IAsyncResult),
                        ByVal endMethod As Func(Of IAsyncResult, TResult),
                        ByVal dataBuffer As TArg1,
                        ByVal byteOffsetToStartAt As TArg2,
                        ByVal maxBytesToRead As TArg3,
                        ByVal stateInfo As Object)
            '</snippet01>
            Return New Task(Of TResult)
        End Function


    End Class
End Namespace

Module Module1

    Class AsyncResult
        Public Sub New()

        End Sub
        '<snippet07>
        Shared Function ReturnTaskFromAsyncResult() As Task(Of String)
            Dim ar As IAsyncResult = DoSomethingAsynchronously()
            Dim t As Task(Of String) = Task(Of String).Factory.FromAsync(ar, Function(res) CStr(res.AsyncState))
            Return t
        End Function
        '</snippet07>

        Private Shared Function DoSomethingAsynchronously() As IAsyncResult
            Throw New NotImplementedException
        End Function

    End Class
    '<snippet08>
    Class WebDataDownLoader

        Dim tcs As New TaskCompletionSource(Of String())
        Dim nameToSearch As String
        Dim token As CancellationToken
        Dim results As New List(Of String)
        Dim m_lock As Object
        Dim count As Integer
        Dim addresses() As String

        Shared Sub Main()

            Dim downloader As New WebDataDownLoader()
            downloader.addresses = {"http://www.msnbc.com", "http://www.yahoo.com", _
                                         "http://www.nytimes.com", "http://www.washingtonpost.com", _
                                         "http://www.latimes.com", "http://www.newsday.com"}
            Dim cts As New CancellationTokenSource()

            ' Create a UI thread from which to cancel the entire operation
            Task.Factory.StartNew(Sub()
                                      Console.WriteLine("Press c to cancel")
                                      If Console.ReadKey().KeyChar = "c"c Then
                                          cts.Cancel()
                                      End If
                                  End Sub)

            ' Using a neutral search term that is sure to get some hits on English web sites.
            ' Please substitute your favorite search term.
            downloader.nameToSearch = "the"
            Dim webTask As Task(Of String()) = downloader.GetWordCounts(downloader.addresses, downloader.nameToSearch, cts.Token)

            ' Do some other work here unless the method has already completed.
            If (webTask.IsCompleted = False) Then
                ' Simulate some work
                Thread.SpinWait(5000000)
            End If

            Dim results As String() = Nothing
            Try
                results = webTask.Result
            Catch ae As AggregateException
                For Each ex As Exception In ae.InnerExceptions
                    If (TypeOf (ex) Is OperationCanceledException) Then
                        Dim oce As OperationCanceledException = CType(ex, OperationCanceledException)
                        If oce.CancellationToken = cts.Token Then
                            Console.WriteLine("Operation canceled by user.")
                        End If
                    Else
                        Console.WriteLine(ex.Message)
                    End If

                Next
            Finally
                cts.Dispose()
            End Try

            If (Not results Is Nothing) Then
                For Each item As String In results
                    Console.WriteLine(item)
                Next
            End If

            Console.WriteLine("Press any key to exit")
            Console.ReadKey()
        End Sub

        Public Function GetWordCounts(ByVal urls() As String, ByVal str As String, ByVal token As CancellationToken) As Task(Of String())

            Dim webClients() As WebClient
            ReDim webClients(urls.Length)
            m_lock = New Object()

            ' If the user cancels the CancellationToken, then we can use the
            ' WebClient's ability to cancel its own async operations.
            token.Register(Sub()
                               For Each wc As WebClient In webClients
                                   If Not wc Is Nothing Then
                                       wc.CancelAsync()
                                   End If
                               Next
                           End Sub)


            For i As Integer = 0 To urls.Length - 1
                webClients(i) = New WebClient()

                ' Specify the callback for the DownloadStringCompleted
                ' event that will be raised by this WebClient instance.
                AddHandler webClients(i).DownloadStringCompleted, AddressOf WebEventHandler

                Dim address As Uri = Nothing
                Try
                    address = New Uri(urls(i))
                    ' Pass the address, and also use it for the userToken 
                    ' to identify the page when the delegate is invoked.
                    webClients(i).DownloadStringAsync(address, address)
                Catch ex As UriFormatException
                    tcs.TrySetException(ex)
                    Return tcs.Task
                End Try

            Next

            ' Return the underlying Task. The client code
            ' waits on the Result property, and handles exceptions
            ' in the try-catch block there.
            Return tcs.Task
        End Function



        Public Sub WebEventHandler(ByVal sender As Object, ByVal args As DownloadStringCompletedEventArgs)

            If args.Cancelled = True Then
                tcs.TrySetCanceled()
                Return
            ElseIf Not args.Error Is Nothing Then
                tcs.TrySetException(args.Error)
                Return
            Else
                ' Split the string into an array of words,
                ' then count the number of elements that match
                ' the search term.
                Dim words() As String = args.Result.Split(" "c)
                Dim NAME As String = nameToSearch.ToUpper()
                Dim nameCount = (From word In words.AsParallel()
                                 Where word.ToUpper().Contains(NAME)
                                 Select word).Count()

                ' Associate the results with the url, and add new string to the array that 
                ' the underlying Task object will return in its Result property.
                results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, nameToSearch))
            End If

            SyncLock (m_lock)
                count = count + 1
                If (count = addresses.Length) Then
                    tcs.TrySetResult(results.ToArray())
                End If
            End SyncLock
        End Sub
        '</snippet08>
    End Class


    '<snippet09>
    Class Calculator
        Public Function BeginCalculate(ByVal decimalPlaces As Integer, ByVal ac As AsyncCallback, ByVal state As Object) As IAsyncResult
            Console.WriteLine("Calling BeginCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId)
            Dim myTask = Task(Of String).Factory.StartNew(Function(obj) Compute(decimalPlaces), state)
            myTask.ContinueWith(Sub(antecedent) ac(myTask))

        End Function
        Private Function Compute(ByVal decimalPlaces As Integer)
            Console.WriteLine("Calling compute on thread {0}", Thread.CurrentThread.ManagedThreadId)

            ' Simulating some heavy work.
            Thread.SpinWait(500000000)

            ' Actual implementation left as exercise for the reader.
            ' Several examples are available on the Web.
            Return "3.14159265358979323846264338327950288"
        End Function

        Public Function EndCalculate(ByVal ar As IAsyncResult) As String
            Console.WriteLine("Calling EndCalculate on thread {0}", Thread.CurrentThread.ManagedThreadId)
            Return CType(ar, Task(Of String)).Result
        End Function
    End Class

    Class CalculatorClient
        Shared decimalPlaces As Integer
        Shared Sub Main()
            Dim calc As New Calculator
            Dim places As Integer = 35
            Dim callback As New AsyncCallback(AddressOf PrintResult)
            Dim ar As IAsyncResult = calc.BeginCalculate(places, callback, calc)

            ' Do some work on this thread while the calculator is busy.
            Console.WriteLine("Working...")
            Thread.SpinWait(500000)
            Console.ReadLine()
        End Sub

        Public Shared Sub PrintResult(ByVal result As IAsyncResult)
            Dim c As Calculator = CType(result.AsyncState, Calculator)
            Dim piString As String = c.EndCalculate(result)
            Console.WriteLine("Calling PrintResult on thread {0}; result = {1}",
                       Thread.CurrentThread.ManagedThreadId, piString)
        End Sub

    End Class
    '</snippet09>

    Class FileStreamDemo
        Shared Sub Main()
            Dim fsd As New FileStreamDemo()
            fsd.ShowCallingFromAsync()
        End Sub

        '<snippet03>
        Const MAX_FILE_SIZE As Integer = 14000000
        Shared Function GetFileStringAsync(ByVal path As String) As Task(Of String)
            Dim fi As New FileInfo(path)
            Dim data(fi.Length - 1) As Byte

            Dim fs As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, True)

            ' Task(Of Integer) returns the number of bytes read
            Dim myTask As Task(Of Integer) = Task(Of Integer).Factory.FromAsync(
                AddressOf fs.BeginRead, AddressOf fs.EndRead, data, 0, data.Length, Nothing)

            ' It is possible to do other work here while waiting
            ' for the antecedent task to complete.
            ' ...

            ' Add the continuation, which returns a Task<string>. 
            Return myTask.ContinueWith(Function(antecedent)
                                           fs.Close()
                                           If (antecedent.Result < 100) Then
                                               Return "Data is too small to bother with."
                                           End If
                                           ' If we did not receive the entire file, the end of the
                                           ' data buffer will contain garbage.
                                           If (antecedent.Result < data.Length) Then
                                               Array.Resize(data, antecedent.Result)
                                           End If

                                           ' Will be returned in the Result property of the Task<string>
                                           ' at some future point after the asynchronous file I/O operation completes.
                                           Return New UTF8Encoding().GetString(data)
                                       End Function)

        End Function
        '</snippet03>

        Public Sub ShowCallingFromAsync()
            Dim path As String = "\\docbuild2\public\Main\Logs\DllDiffReport\DllDiffReport_BetweenInBuildAndInPublicShare.html"
            '<snippet04>
            Dim myTask As Task(Of String) = GetFileStringAsync(path)

            ' Do some other work
            ' ...

            Try
                Console.WriteLine(myTask.Result.Substring(0, 500))
            Catch ex As AggregateException
                Console.WriteLine(ex.InnerException.Message)
            End Try
            '</snippet04>

            Console.WriteLine("Press Enter to exit.")
            Console.ReadLine()
        End Sub

        '<snippet05>
        Public Function GetFileStringAsync2(ByVal path As String) As Task(Of String)
            Dim fi = New FileInfo(path)
            Dim data(fi.Length - 1) As Byte
            Dim state As New MyCustomState()

            Dim fs As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, data.Length, True)
            ' We still pass null for the last parameter because
            ' the state variable is visible to the continuation delegate.
            Dim myTask As Task(Of Integer) = Task(Of Integer).Factory.FromAsync(
                    AddressOf fs.BeginRead, AddressOf fs.EndRead, data, 0, data.Length, Nothing)

            Return myTask.ContinueWith(Function(antecedent)
                                           fs.Close()
                                           ' Capture custom state data directly in the user delegate.
                                           ' No need to pass it through the FromAsync method.
                                           If (state.StateData.Contains("New York, New York")) Then
                                               Return "Start spreading the news!"
                                           End If

                                           ' If we did not receive the entire file, the end of the
                                           ' data buffer will contain garbage.
                                           If (antecedent.Result < data.Length) Then
                                               Array.Resize(data, antecedent.Result)
                                           End If
                                           '/ Will be returned in the Result property of the Task<string>
                                           '/ at some future point after the asynchronous file I/O operation completes.
                                           Return New UTF8Encoding().GetString(data)
                                       End Function)

        End Function
        '</snippet05>


        '<snippet06>
        Public Function GetMultiFileData(ByVal filesToRead As String()) As Task(Of String)
            Dim fs As FileStream
            Dim tasks(filesToRead.Length - 1) As Task(Of String)
            Dim fileData() As Byte = Nothing
            For i As Integer = 0 To filesToRead.Length
                fileData(&H1000) = New Byte()
                fs = New FileStream(filesToRead(i), FileMode.Open, FileAccess.Read, FileShare.Read, fileData.Length, True)

                ' By adding the continuation here, the 
                ' Result of each task will be a string.
                tasks(i) = Task(Of Integer).Factory.FromAsync(AddressOf fs.BeginRead,
                                                              AddressOf fs.EndRead,
                                                              fileData,
                                                              0,
                                                              fileData.Length,
                                                              Nothing).
                                                          ContinueWith(Function(antecedent)
                                                                           fs.Close()
                                                                           'If we did not receive the entire file, the end of the
                                                                           ' data buffer will contain garbage.
                                                                           If (antecedent.Result < fileData.Length) Then
                                                                               ReDim Preserve fileData(antecedent.Result)
                                                                           End If

                                                                           'Will be returned in the Result property of the Task<string>
                                                                           ' at some future point after the asynchronous file I/O operation completes.
                                                                           Return New UTF8Encoding().GetString(fileData)
                                                                       End Function)
            Next

            Return Task(Of String).Factory.ContinueWhenAll(tasks, Function(data)

                                                                      ' Propagate all exceptions and mark all faulted tasks as observed.
                                                                      Task.WaitAll(data)

                                                                      ' Combine the results from all tasks.
                                                                      Dim sb As New StringBuilder()
                                                                      For Each t As Task(Of String) In data
                                                                          sb.Append(t.Result)
                                                                      Next
                                                                      ' Final result to be returned eventually on the calling thread.
                                                                      Return sb.ToString()
                                                                  End Function)
        End Function
        '</snippet06>

        Class MyCustomState
            Public StateData As String
            Public Sub New()
                StateData = "Hello"
            End Sub
        End Class


    End Class


End Module

Use the StreamExtensions sample code

The StreamExtensions.cs file, in the .NET Standard parallel extensions extras repository, contains several reference implementations that use Task objects for asynchronous file and network I/O.

See also