[問題] C# 調用 C++ dll 的迴圈內資料

看板C_Sharp (C#)作者 (b26168)時間8年前 (2017/03/20 22:13), 8年前編輯推噓1(104)
留言5則, 3人參與, 最新討論串1/1
目前用 C++ 寫一個供 C# wpf UI 調用的 API 假設某個 C++ 的 loop 中 要每次都回傳某個資料(數值或字串等)出來給 C# 即時使用 (例如 更新 progress bar 或 show 進度的字串) 會有哪些建議的做法呢? Console.OpenStandardInput 配合 backgroundworker? Task? 或是 其他較適合 MVVM 架構的做法? 感謝~ -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.42.50.191 ※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1490019188.A.B0C.html

03/20 23:56, , 1F
delegate(C#)->function pointer(C)的方式處理進度?
03/20 23:56, 1F
我嘗試在 c++ loop 中 cout 出來 並使用 Task.Run( () => { Process p = Process.GetCurrentProcess(); p.StartInfo.RedirectStandardOutput = true; p.StartInfo.UseShellExecute = false; while (true) { StreamReader sr = p.StandardOutput; string strconsole = sr.ReadLine(); if (strconsole.Equals("Done")) break; } }); 不過 StandardOutput 卻出現錯誤 於 System.InvalidOperationException 擲回例外狀況: 'System.dll' 'System.InvalidOperationException' 類型的例外狀況發生於 System.dll,但使用者程 式碼未加以處理 其他資訊: StandardOut 尚未重新導向,或者處理序尚未啟動。 無法這樣使用嗎? ※ 編輯: b26168 (220.128.199.139), 03/21/2017 10:28:32

03/21 20:57, , 2F
你是想抓C# call C時Output的字串? 私心不建議這樣的模式
03/21 20:57, 2F

03/22 00:32, , 3F
你是想抓自己程式link的C++ DLL的StdOut?
03/22 00:32, 3F

03/22 00:40, , 4F
試試看用 SetStdHandle API 把-11或-12導到C#的PipeStream
03/22 00:40, 4F

04/12 18:30, , 5F
怎不把流程控制放在上層
04/12 18:30, 5F
文章代碼(AID): #1Op-DqiC (C_Sharp)
文章代碼(AID): #1Op-DqiC (C_Sharp)