Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataContractSerializer in WinRT

I was practicing the WinRT API but encountered some problems need your help.

I want to try the DataContractSerializer and reference 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 most weird thing is that if I only use:

Stream inStream = readStream.OpenRead());

and I pass this stream into:

DataContractSerializer.WriteObject

The API will be stuck forever.

But if I use:

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

And pass this stream into the WriteObject then it will work fine.

I have no idea why this symptom only occurred if I don't use Task.Run and await for the stream.

Could any one give me some advice or suggestion?


But

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

I don't know why I need to create Task on purpose to do this.

Thanks.

like image 326
user588477 Avatar asked Nov 11 '11 14:11

user588477


1 Answers

The answer is in the project description:

"ObjectStorageHelper is a Generic class that simplifies storage of data in WinRT applications while still maintaining the async *principles* of Metro-style apps."

All File/IO operations in WinRT are by nature asynchronous, hence the need to use methods that are also asynchronous (in order to get any result, at least). The new "await" keyword is one way to accomplish this, although you could also explicitly assign a callback function to handle the completion of the async operation.

like image 75
Kirkaiya Avatar answered Sep 18 '22 03:09

Kirkaiya