The blank ( no text ) console window is visible once i start the process but it doesn't seem to send out commands.
Here is how i define my 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 );
}
};
}
Starting the thread
Thread thread;
Process process;
public void Start()
{
Init();
thread = new Thread(ThreadMain);
thread.Start();
}
Register external commands for execution
string command;
bool command_complete = true;
public void Exec( string command )
{
if( ! command_complete ) return;
this.command = command;
command_complete = string.IsNullOrWhiteSpace( command );
}
My events:
public event Action<string> ErrorDataReceived;
public event Action<string> OutputDataReceived;
public event Action<string> InputDataReceived;
Her i am able to see the InputDataReceived being called from the outside, but never 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();
}
Just had a look at this post , looks like i forgot to begin input monitoring
void ThreadMain()
{
process.BeginOutputReadLine();
process.BeginErrorReadLine();
while( true )
{
// ...
Thread.Sleep( 250 );
}
process.CancelOutputRead();
process.CancelErrorRead();
Dispose();
}
Try this
if (!command_complete)
{
using (StreamWriter si = process.StandardInput)
{
si.WriteLine(command);
}
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.