DataContractSerializer in WinRT

I practiced the WinRT API, but ran into some problems and you need your help.

I want to try DataContractSerializer and link to this site:

http://winrtstoragehelper.codeplex.com/

The code:

 Stream inStream = Task.Run(() => readStream.OpenRead()).Result; 

I think it should be (bug?):

 Stream inStream = await Task.Run(() => readStream.OpenRead()); 

But the strangest thing is that if I only use:

 Stream inStream = readStream.OpenRead()); 

and I pass this stream to:

 DataContractSerializer.WriteObject 

The API is stuck forever.

But if I use:

 Stream inStream = await Task.Run(() => readStream.OpenRead()); 

And pass this stream to WriteObject , then it will work fine.

I have no idea why this symptom occurred if I do not use Task.Run and await for the thread.

Can someone give me advice or suggestion?


But

Stream inStream = readStream.OpenRead () method was not named "async"

I do not know why I need to create a Task for this.

Thanks.

+6
source share
1 answer

The answer is given in the project description:

"ObjectStorageHelper is a general class that simplifies data storage in WinRT applications, and also supports async * principles * of Metro style applications."

All File / IO operations in WinRT are inherently asynchronous, so you must use methods that are also asynchronous (in order to get at least some result). The new wait keyword is one way to achieve this, although you can also explicitly assign a callback function to handle the completion of an async operation.

+4
source

Source: https://habr.com/ru/post/901302/


All Articles