Downloading data in MemoryStream

blazor

Question

I have a google table and download it:

 Task<byte[]> _res = Http.GetByteArrayAsync(_webPath);

Then I make a Stream:

Stream _stream = new MemoryStream(_res.Result);

And work with this stream. I want to parse table. In console .net core application this method work good, but when i use it in blazor app, it doesn't work.

Task<byte[]> _res = Http.GetByteArrayAsync(_webPath);
Console.WriteLine("3");
_stream = new MemoryStream(_res.Result);
Console.WriteLine("4");

Result

I haven't exception.

Accepted Answer

The problem is that _res.Result blocks the thread until the task is completed which results in a deadlock as blazor only has one thread.

https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1.result?view=netframework-4.7.2

Accessing the property's get accessor blocks the calling thread until the asynchronous operation is complete; it is equivalent to calling the Wait method.

You need to await the task with await _res so that the thread isn't block and can complete the task.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why