c# - 想通过本地node程序运行js代码,但是阻塞了?
本文介绍了c# - 想通过本地node程序运行js代码,但是阻塞了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
这是我的代码
private void RunNode(string code)
{
Process scriptProc = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "node.exe";
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
info.CreateNoWindow = false;
scriptProc.StartInfo = info;
scriptProc.Start();
scriptProc.StandardInput.WriteLine(code + "&.exit");
string outStr = scriptProc.StandardOutput.ReadToEnd();
scriptProc.Close();
}
我的程序界面
调试发现执行到
string outStr = scriptProc.StandardOutput.ReadToEnd();这里阻塞了
看来是没退出。。
解决方案
自己解决了哦,要关闭流
private void RunNode(string code)
{
Process scriptProc = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "node.exe";
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
info.CreateNoWindow = false;
scriptProc.StartInfo = info;
scriptProc.Start();
StreamWriter writer = scriptProc.StandardInput;
writer.WriteLine(code);
writer.Close();
string outStr = scriptProc.StandardOutput.ReadToEnd();
scriptProc.Close();
}
这篇关于c# - 想通过本地node程序运行js代码,但是阻塞了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文