r wank
r wank

Reputation: 352

Using an Async task to read from a filestream instead of an endless while loop

Its a pretty simple program. I'm trying to read out messages from a controller after I send a message that requests constant messages to be sent back. The first part is how I managed to get the reading working as intended, and every time a message is sent I can see it in the Console. I want to recreate this scenario with the task in the code below that. How ever I haven't been successful in doing so.

            if (success)
            {
                if (fileStreamDeviceData.CanRead)
                {
                    while(success)
                    {
                        await fileStreamDeviceData.ReadAsync(outputReportBuffer, 0, outputReportBuffer.Length);
                        for (int b = 0; b < 12; b++)
                        {
                            Console.Write(outputReportBuffer[b] + " ");
                        }
                        Console.WriteLine();

                    }
                }
            }

So the above part works as intended except for the ugly endless while loop that is currently happening. The following 2 Code Parts are the solution for this, but its not working as intended and I don't seem to get anything out of it. I really suck with async tasks and stuff so maybe someone can help me fix the below section.

            if (success)
            {

                outputReportBuffer = null;
                //start async reading
                try
                {
                    outputReportBuffer = await ReadByteAsync();
                    for (int b = 0; b < outputReportBuffer.Length; b++)
                    {
                        Console.Write(outputReportBuffer[b] + " ");
                    }
                    Console.WriteLine();

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }


    private async Task<Byte[]> ReadByteAsync()
    {
        Byte[] result = null;
        Byte[] outputReportBuffer = null;

        using (fileStreamDeviceData)
        {
            outputReportBuffer = new Byte[MyHid.Capabilities.OutputReportByteLength];

            int byteRead;
            while ((byteRead = await fileStreamDeviceData.ReadAsync(outputReportBuffer, 0, outputReportBuffer.Length)) != 0)
            {
                result = outputReportBuffer;
            }

            return result;
        }
    }

I get no exception or any output when I use this Code.

Upvotes: 0

Views: 1326

Answers (1)

Joseph Jeganathan
Joseph Jeganathan

Reputation: 234

I can be due to many reasons, try to print something int he while-loop first.

Does the caller outputReportBuffer = await ReadByteAsync(); is inside an asyn method itself? If not I'll use ReadByteAsync().Result instead and set ConfigureAwait(false) in the ReadByteAsync() method.

    const int BufferSize = 100;

    private async Task<Byte[]> ReadByteAsync()
    {
        var memoryStream = new MemoryStream();

        using (fileStreamDeviceData)
        {
            var outputReportBuffer = new Byte[BufferSize];
            int offset = 0;
            int byteRead;

            while ((byteRead = await fileStreamDeviceData.ReadAsync(outputReportBuffer, offset, outputReportBuffer.Length)) != 0)
            {
                memoryStream.Write(outputReportBuffer, offset, byteRead);
                offset += BufferSize;
            }

            return memoryStream.ToArray();
        }
    }

Upvotes: 1

Related Questions