C#进程线程无法写入输出

时间:2019-05-21 19:34:36

标签: c# windows multithreading terminal process

启动该过程后,空白(无文本)控制台窗口可见,但似乎没有发出命令。

这是我定义流程的方式:

        void Init()
        {
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = "CMD.exe";
            // psi.FileName = @"cmd.exe";
            // psi.Arguments = @"'/K' C:\Dev\Anaconda3\Scripts\activate.bat C:\Dev\Anaconda3";
            psi.RedirectStandardInput = true;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError = true;
            // psi.WorkingDirectory = @"C:\temp";
            psi.UseShellExecute = false;
            // psi.CreateNoWindow = true;
            // psi.WindowStyle = ProcessWindowStyle.Hidden;
            process = Process.Start( psi );

            process.ErrorDataReceived += ( sender, e ) => 
            { 
                if( ErrorDataReceived != null )
                {
                    ErrorDataReceived.Invoke( e.Data );
                }
            };
            process.OutputDataReceived += ( sender, e ) => 
            { 
                if( OutputDataReceived != null )
                {
                    OutputDataReceived.Invoke( e.Data );
                }
            };
        }

启动线程

        Thread thread;
        Process process;
        public void Start()
        {
            Init();
            thread = new Thread(ThreadMain);
            thread.Start(); 
        }

注册要执行的外部命令

        string command;
        bool command_complete = true;
        public void Exec( string command )
        {
            if( ! command_complete ) return;
            this.command = command;
            command_complete = string.IsNullOrWhiteSpace( command );
        }

我的活动:

        public event Action<string> ErrorDataReceived;
        public event Action<string> OutputDataReceived;
        public event Action<string> InputDataReceived;

我可以看到从外面调用InputDataReceived,但是从没有看到OutputDataReceived

        void ThreadMain()
        {
            while( true )
            {
                if( exit ) break;

                if( ! command_complete )
                {
                    process.StandardInput.WriteLine( command );

                    if( InputDataReceived != null )
                    {
                        InputDataReceived.Invoke( command );
                    }

                    command_complete = true;
                }

                Thread.Sleep( 250 );
            }

            Dispose();
        }

2 个答案:

答案 0 :(得分:1)

只看了this post,好像我忘了开始监视输入

num_cols = 99

for i in range(num_cols): # or however many columns you want to add
    df[i] = df.x * random.randint(a,b)

df.head()
     x        0        1       2        3        4        5        6  ...      92      93      94       95       96       97      98       99
0   68   257040   214268  107576   266152   229568   309468   319668  ...   74460   25024   85952   320620   331840   175712   87788   254864
1  286  1081080   901186  452452  1119404   965536  1301586  1344486  ...  313170  105248  361504  1348490  1395680   739024  369226  1071928
2  421  1591380  1326571  666022  1647794  1421296  1915971  1979121  ...  460995  154928  532144  1985015  2054480  1087864  543511  1577908
3   13    49140    40963   20566    50882    43888    59163    61113  ...   14235    4784   16432    61295    63440    33592   16783    48724
4  344  1300320  1083944  544208  1346416  1161344  1565544  1617144  ...  376680  126592  434816  1621960  1678720   888896  444104  1289312

[5 rows x 101 columns]

答案 1 :(得分:1)

尝试一下

if (!command_complete)
   {
       using (StreamWriter si = process.StandardInput)
       {
           si.WriteLine(command);
       }
       ...
   }