作者b26168 (b26168)
看板C_Sharp
标题[问题] C# 调用 C++ dll 的回圈内资料
时间Mon Mar 20 22:13:05 2017
目前用 C++ 写一个供 C# wpf UI 调用的 API
假设某个 C++ 的 loop 中
要每次都回传某个资料(数值或字串等)出来给 C# 即时使用
(例如 更新 progress bar 或 show 进度的字串)
会有哪些建议的做法呢?
Console.OpenStandardInput 配合 backgroundworker? Task?
或是 其他较适合 MVVM 架构的做法?
感谢~
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.42.50.191
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1490019188.A.B0C.html
1F:推 yeo1987: delegate(C#)->function pointer(C)的方式处理进度? 03/20 23:56
我尝试在 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
2F:→ yeo1987: 你是想抓C# call C时Output的字串? 私心不建议这样的模式 03/21 20:57
3F:→ Litfal: 你是想抓自己程式link的C++ DLL的StdOut? 03/22 00:32
4F:→ Litfal: 试试看用 SetStdHandle API 把-11或-12导到C#的PipeStream 03/22 00:40
5F:→ disabledman: 怎不把流程控制放在上层 04/12 18:30