My program has a performance problem

2

I made this program with the help of the user ramaral. It is a Windows Forms program and has to run another console. The issue is that I need instead of that other program to open the console, it will be hidden (without CMD window) and my program will capture the texts that the other would write on the screen.

As it is now, it is opening the console window (without text) and it takes me 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: The console application does not require 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 });
}
    
asked by anonymous 12.07.2015 / 02:37

1 answer

2

The program takes a long time to display the data because it gets stuck waiting for the other to finish.

Do this:

// 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;

    //Inicia o outro programa em outra thread
    Task.Factory.StartNew(() => StartProcess(this.VBox));

}

//Método que inicia outro processo
public void StartProcess(string fileName)
{
    Process p = new Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.FileName = fileName;
    p.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);

    p.Start();
    p.BeginOutputReadLine();
    p.WaitForExit();
    p.Close();
}

// Essa é o evento que acontece quando o aplicativo de console escreve algo na tela
private void OutputHandler(object process, DataReceivedEventArgs e)
{
    if (!String.IsNullOrEmpty(e.Data))
    {
        //Invoca a main thread para actualizar o TextBox
        BeginInvoke(new MethodInvoker(delegate
        {
            this.Saida.Text += e.Data;
        }));
    }
}
    
12.07.2015 / 13:28