C#进程线程不能写输出

Posted

技术标签:

【中文标题】C#进程线程不能写输出【英文标题】:C# process thread can't write output 【发布时间】:2019-10-08 06:15:26 【问题描述】:

一旦我启动进程,空白(无文本)控制台窗口就可见,但它似乎没有发出命令。

这是我如何定义我的流程:

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

【问题讨论】:

【参考方案1】:

试试这个

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

【讨论】:

刚刚也试过你的方法,它让我ObjectDisposedException: Cannot write to a closed TextWriter不确定发生了什么:\ 我不知道它是否适合你,但尝试将 RedirectStandardOutput 和 RedirectStandardError 设置为 false 然后使用 process.StandardInput.WriteLine(command) (而不是使用)【参考方案2】:

刚看了this post,好像忘记开始输入监控了

        void ThreadMain()
        
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();

            while( true ) 
             
                // ...

                Thread.Sleep( 250 ); 
             

            process.CancelOutputRead();
            process.CancelErrorRead();

            Dispose();
        

【讨论】:

以上是关于C#进程线程不能写输出的主要内容,如果未能解决你的问题,请参考以下文章

C# 线程

C#:进程线程应用程序域(AppDomain)与上下文分析

c#线程停止

C# 进程线程和多线程

如何在 Windows 窗体中分离 UI 线程和进程线程

c#异步编程-线程