看板 C_Sharp 關於我們 聯絡資訊
各位好 不曉得各位版大 有沒有使用過C# 5.0 最近有在練習 之前有用過DataContractSerializer的API 想要在win8 winRT上寫寫看 我遇到一些問題 於是google了一下 http://winrtstoragehelper.codeplex.com/ 請各位幫忙看一下 他有一段code非常詭異 Stream inStream = Task.Run(() => readStream.OpenRead()).Result; 我覺得該寫成 Stream inStream = await Task.Run(() => readStream.OpenRead()); 因為要真的等到他做完, 懷疑他寫錯 是bug 但是其實我一開始是寫成 Stream inStream = readStream.OpenRead(); 畢竟這API不是Async的 結果我把這inStream塞到 DataContractSerializer::WriteObject裡面 這API就完全的卡住了 程式Hang住 但我改成 Stream inStream = await Task.Run(() => readStream.OpenRead()); 神奇的就不會hang了 我想了很久 完全無法參透這問題... 開Task.Run做事結束後再執行 跟 直接呼叫 做完 竟然有差異 請問有人知道為啥嗎 或是給點意見@@ http://tinyurl.com/6mmgcsm 參考這網頁 也有人提問 但沒回應 請大家看一下 感謝~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 27.246.206.82
LaPass:改版的好快.... 一陣子沒寫C#,就又升級了 = =" 11/12 02:37
james732:C#的版本號碼快要趕上Java了...XDD 11/12 15:52
ngulin0911:推改版的好快...... 11/12 22:52
deuter:Task.Result 就已經會等做完才傳回result 11/14 00:59
deuter:await不是指"等"他做完, 而是指把後面剩下的 statements 11/14 01:00
deuter:加到continuation 11/14 01:00
QQ29:await 應該會等Task.Run這Task做完吧? 然後才會下一行 11/14 12:12
QQ29:但照理講 以現在的code來看 我沒必要開Task.Run不是嗎 11/14 12:13
deuter:await不會等做完, 否則就失去async的意義了 11/14 15:54
deuter:http://goo.gl/l5US 看起來應該不用開 Task.Run也可以 11/14 15:55
deuter:不過沒用過StorageFile class 11/14 15:55
QQ29:我認為會等他做完耶 如果Task是worker thread 會先讓UIthread 11/14 17:06
QQ29:去做事阿 11/14 17:07
QQ29:請問Task.Result 會等他做完才回傳 是哪裡有document呀 11/14 17:07
deuter:沒寫清楚,await是等做完後再接下去執行後面的程式,但是會 11/15 12:56
deuter:馬上return caller, 不會block current thread 11/15 12:56
deuter:"The get accessor for this property ensures that the 11/15 12:57
deuter:asynchronous operation is complete before returning." 11/15 12:57