Dependency Error

Asked

Viewed 238 times

2

I’m using the BackgroundWorker

private BackgroundWorker BGWorker = new BackgroundWorker();
BGWorker.DoWork += BGWorker_DoWork;
BGWorker.RunWorkerAsync();

private void BGWorker_DoWork(object sender, DoWorkEventArgs e)
{
   ObterInformacoes();
}

private void BGWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    dgv.ItemsSource = ObterInformacoes();
}

private List<Informacoes> ObterInformacoes()
{
    List<Informacoes> func = gestores.gInfo.Recuperar(new Filtro[] { 
        new Filtro(eInfo.Ativa, true),
        new Filtro(eInfo.Recorrente, false)
    }).Select(f => new Informacoes(f)).ToList();

    return func.OrderBy(f => f.Nome).AsParallel().ToList();
}

And it’s returning to me this mistake:

Additional information: It is necessary to create Dependencysource in the same Thread that Dependencyobject.

Does anyone know what it can be?

1 answer

3


I didn’t see the need to use the Backgroundworker in this example, since you call the method ObterInformacoes() in the method _DoWork from BW and then call him again at _RunWorkerCompleted.

this could be improved as follows:

private BackgroundWorker BGWorker = new BackgroundWorker();
BGWorker.DoWork += BGWorker_DoWork;
BGWorker.ReportProgress = true; //Atenção para esta linha
BGWorker.RunWorkerAsync();

private void BGWorker_DoWork(object sender, DoWorkEventArgs e)
{
   e.Result = ObterInformacoes();
}

private void BGWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    var lista = e.Result as List<Informacoes>;
    dgv.ItemsSource = lista;
}

private List<Informacoes> ObterInformacoes()
{
    List<Informacoes> func = gestores.gInfo.Recuperar(new Filtro[] { 
        new Filtro(eInfo.Ativa, true),
        new Filtro(eInfo.Recorrente, false)
    }).Select(f => new Informacoes(f)).OrderBy(f => f.Nome).ToList();

    return func;
}

I believe this should work.

  • The list processing is done in the seprada thread, using the BackGroundWorker and upon completion of processing he notifies that he has finished passing the list as Result to the BGWorker_RunWorkerCompleted responsible for displaying the data processed.

  • @Richarddias I may be wrong, but I believe your intention in the method BGWorker_RunWorkerCompleted was to write dgv.ItemsSource = lista, instead of calling again the method ObterInformacoes. Also, instead of creating the var lista in BGWorker_DoWork, the correct would not be to assign the result to the property e.Result?

  • That’s right @Conradclark. Thank you very much for the comments. I’ve updated the response.

  • Are you using the AsParallel still? Post more details about the error.

  • But in doing so, it makes no sense to use the BackGroundWorker

  • Add more information about: gestores.gInfo

  • I managed to solve the problem in my class Informacoes I have a property that is a photo. I added Foto.Freeze() and is working properly. I am using the e.Result as suggested in the reply! Thank you.

Show 2 more comments

Browser other questions tagged

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