Dependency Error

2

I'm using 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 this error is returning me:

  

Additional information: You must create DependencySource in the same   Thread that DependencyObject.

Does anyone know what it can be?

    
asked by anonymous 05.03.2015 / 19:25

1 answer

3

I did not see the need to use BackGroundWorker in this example, since you call the ObterInformacoes() method in the% BW% method and then call it again in _DoWork .

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.

    
05.03.2015 / 20:02