C# Backgroundworker

Asked

Viewed 213 times

-1

Hello, I am with the following message, it is the first time q I am working with Backgroundworker and I run a long function inside the function (Dowork) of the Backgroundworker however it happens this message and does not run this lbMSG is a LABEL where I show some messages, So what I did I commented the LABEL but ai error in other parts with the same message. ( System.Invalidoperationexception: 'Invalid threading operation: 'lbMSG' control accessed from a thread that is not the one in which it was created.' )

This is the code of (Dowork)

       private void bcwLeDbf_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        //Executa a tarefa
        ConectaDBF("select * from " + CaminhoArquivo);
        //Verifica se houve uma requisição para cancelar a operação.
        if (bcwLeDbf.CancellationPending)
        {
            //se sim, define a propriedade Cancel para true
            //para que o evento WorkerCompleted saiba que a tarefa foi cancelada.
            e.Cancel = true;
            return;
        }
    }

If anyone has any tips thank you.

  • you have the Connected variablebf and Hike?

  • 1

    Backgroundworker works on a thread other than the main thread of the application where the visual part is created... so you can’t just access a control that was created in the main thread from within the backgroundworker. For an appropriate response, you should have the method code ConectaDBF without this I cannot point out where the error is. Take a look at other examples of background worker, and how to use the report Progress. There is also the option to use the Invoke: https://answall.com/a/324491/69359

1 answer

1


Probably on the property CaminhoArquivo or in the method ConectaDBF you are searching for the values of the screen. I am assuming this because information is missing from your code snippet.

But the point is, this is not possible. Because memory references do not exist in the thread running the event DoWork.

To do this operation, you should pass the values per parameter in the execution of your BackgroundWorker.

Example:

//Aqui você passa os valores da sua tela por parametro
backgroundWorker1.RunWorkerAsync(CaminhoArquivo);

And within your Dowork method, you take the value as follows:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    //Executa a tarefa
    ConectaDBF("select * from " + (string)e.Argument); //Aqui você pega pelo e.Argument
}

Another point is that you can use the event ProgressChanged which will run on the main thread of your application.

For that, just make a call like this:

backgroundWorker1.ReportProgress(10);

Don’t forget to change the property WorkerReportsProgress for true, if you want to use this example above.

  • In fact it does not arrive at the assignment of the variable Camioarquivo the Label is the first thing q this function Conectadbf makes.

Browser other questions tagged

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