想通过本地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();
}

我的程序界面

clipboard.png
调试发现执行到
string outStr = scriptProc.StandardOutput.ReadToEnd();这里阻塞了
看来是没退出。。

阅读 2.8k
2 个回答

自己解决了哦,要关闭流

 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();
            
        }

如果只是执行简单的js函数的话,可以考虑使用jint.net,新版本开始兼容es6了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进