2
I made this program with the help of the ramaral user. It is a Windows Forms program and has to run another console. The point is that I need that instead of this other program to open the console, it is hidden (without CMD window) and my program captures the texts that the other would write on the screen.
The way it is now, it is opening the console window (with no text at all) and mine takes a long time to display the captured texts and loses some of them in the process, and the console application does nothing but write messages from 50 to 100 lines in total.
Note: Console application does not need user intervention.
// Esse método que inicia todo o processo
private void Confirmar_Click(object sender, EventArgs e)
{
this.Saida.Text = "Linha de comando: VboxManage " +
this.Parametros.Text + Environment.NewLine + Environment.NewLine;
Process processo = new Process();
processo.StartInfo.UseShellExecute = false;
processo.StartInfo.RedirectStandardOutput = true;
processo.StartInfo.FileName = this.VBox;
processo.OutputDataReceived += new DataReceivedEventHandler(InsercaoNaTela);
processo.Start();
processo.BeginOutputReadLine();
processo.WaitForExit();
processo.Close();
}
// Essa é o evento que acontece quando o aplicativo de console escreve algo na tela
private void InsercaoNaTela(object obj, DataReceivedEventArgs Texto)
{
this.Tarefa = new Thread(FuncaoTarefa);
// Não sei se tem como usar um método com parâmetros,
// então eu uso um atributo da classe.
// Texto.Data é o texto capturado do console
this.Externa = Texto.Data;
this.Tarefa.Start();
}
public delegate void Delegado(String Testo);
public Delegado Delegar;
private Thread Tarefa;
private string Externa;
public void Adicionar(String Texto)
{
// Finalmente a Thread principal escreve o texto capturado no meu TextBox
this.Saida.Text += Texto;
}
private void FuncaoTarefa()
{
// Invoca o delegado passando o texto como parâmetro
// Que eu saiba tem que usar um delegado pra alterar
// controles criados por outras Threads
this.Invoke(this.Delegar, new Object[] { this.Externa });
}
There in the other question, when I read your comment, I thought the problem was another. Now that you posted your code it became clear.
– ramaral