My program has performance issues

Asked

Viewed 103 times

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.

1 answer

2


The program takes a long time to present the data because it is blocked waiting for the other to finish.

Do so:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.