chesh111re
chesh111re

Reputation: 194

Downloading data in MemoryStream

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.

Upvotes: 0

Views: 737

Answers (1)

Nunction
Nunction

Reputation: 26

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://learn.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.

Upvotes: 1

Related Questions