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

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 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试