Link to home
Start Free TrialLog in
Avatar of Wisnu
WisnuFlag for Indonesia

asked on

Stream.BeginRead and Stream.EndRead in .NET Core

Where is Stream.BeginRead and Stream.EndRead method in .NET Core? Do I have to download another nuget package or have to use another class?

User generated image
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Avatar of Wisnu

ASKER

Hi Andy,

Thank for the answer. Indeed, they exist in .NET Framework, however when I port the code to .NET Core, the compile error occurs. Here is my code:

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

private static Task<int> ReadFileAsync(string filePath)
{
    Stream fs = File.OpenRead(filePath);
    byte[] readBuffer = new Byte[fs.Length];
    Task<int> readTask =
        Task.Factory.FromAsync(
            (Func<byte[],
                int,
                int,
                AsyncCallback,
                object,
                IAsyncResult>)
                    fs.BeginRead,
            (Func<IAsyncResult, int>)
                fs.EndRead,
            readBuffer,
            0,
            (int)fs.Length,
            null);

    readTask.ContinueWith(task =>
    {
        if (task.Status == TaskStatus.RanToCompletion)
        {
            IsFinish = true;
            Console.WriteLine(
                "Read {0} bytes from file {1}",
                task.Result,
                filePath);
        }

        fs.Dispose();
    });

    return readTask;
}

Open in new window

Ahhh - now I understand what you meant.  I don't know what (or even if it exists) you would need in that case.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Wisnu

ASKER

thanks Saige, this is what I am looking for.