haifengj 2017-04-10 03:20 采纳率: 66.7%
浏览 2222
已采纳

C# 调用代码的exe,遇到console.Readline获取ReadToEnd返回值时卡死

我现在再弄一个编程程序,当我把以下代码,转换成exe 去调用时候
using System;
using System.Collections.Generic;
using System.Text;

namespace Demo1aaa
{
class Program
{
private void Func(int[] Arg)
{

        for (int i = 1; i < Arg.Length; i++)
        {
            int a = 0;  

            for (int j = 0; j < Arg.Length-i; j++)
            {
                if (Arg[j] > Arg[j + 1])
                {
                    a = Arg[j + 1];
                    Arg[j + 1] = Arg[j];
                    Arg[j] = a;
                }
            }

            for (int k = 0; k < Arg.Length; k++)
            {
                Console.WriteLine(Arg[k]);
            }
            Console.ReadLine();
        }       
    }
    static void Main(string[] args)
    {
        Program p = new Program();
        int[] Arg = { 130, 28, 345, 299, 40, 30, 201 };
        p.Func(Arg);
    }
}

}
我使用下面代码去调用的时候exe 的时候
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = path;
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
p.Start();//启动程序
p.StandardInput.WriteLine("exit");
//p.StandardOutput.EndOfStream

             string txtresult = p.StandardOutput.ReadToEnd();

        因为存在  Console.ReadLine(); 这个代码,所以得到返回值的时候会一直死掉
             加了这个   p.StandardInput.WriteLine("exit");也没用。有哪位大神,遇到过这总情况,麻烦指点一下
  • 写回答

2条回答

  • threenewbee 2017-04-10 04:01
    关注

    p.StandardInput.WriteLine是可以的,问题是Console.ReadLine();放在循环里,有多个,所以你写也要写超过这么多次,好比你人工执行,你需要按下多个回车键,才能继续

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料