1
I’m having a problem invoking a Uithread on PCL.
I made a method like this:
private void PopularConteudo()
{
Task<List<Departamento>> taskDep = new Task<List<Departamento>>(() =>
{
departamentos = departamentoDal.GetAll();
return departamentos.ToList();
});
taskDep.Start();
taskDep.Wait();
PopularCarouselDepartamentos();
Task<List<Categoria>> taskCat = new Task<List<Categoria>>(() =>
{
categorias = categoriaDal.GetAllByDepartamento(carouselViewDepartamentos.Item as Departamento).ToList();
PopularCarouselCategorias();
return categorias.ToList();
});
taskCat.Start();
taskCat.Wait();
Task<List<Produto>> taskProd = new Task<List<Produto>>(() =>
{
produtos = produtoDal.GetAllByCategoria(carouselViewCategorias.Item as Categoria);
return produtos.ToList();
});
taskProd.Start();
taskProd.Wait();
PopularListViewProdutos();
}
private void PopularCarouselDepartamentos()
{
Device.BeginInvokeOnMainThread(()=> {
try
{
carouselViewDepartamentos.ItemsSource = departamentos;
}
catch (Exception e) // handle whatever exceptions you expect
{
//Handle exceptions
}
});
}
What happens is this, when I invoke the Popularcarouseldepartments() method it simply doesn’t enter the Try-catch instruction block.
Consequently Carousel is not populated and I need it to popular other content.
All popular methods are very similar.
Does anyone know how to solve this problem?
The Popularconteudo method is async but at no time there is a await within it. Perhaps this may be one of the problems. I would think a refactoring where the query methods (Get something) were already asynchronous, I believe it would be simpler to manage it :)
– Angelo Belchior
Sorry, I copied the wrong code, the new one is already without async.
– Leonan Fraga Leonardo
What I think is nice is that it is running all the Tasks before performing the Begininvokeonmainthread() method, however I need to stop the Task when it popular my list to later invoke in the Thread UI the list update. However this is not happening.
– Leonan Fraga Leonardo
Run synchronously to test!
– Angelo Belchior
How can I do ? Just call the task.Runsynchronously().
– Leonan Fraga Leonardo
Call the categorieDal and productsDal normally, outside the Task, take this test...
– Angelo Belchior
Good morning friend, I did and did not work, the code keeps skipping the task of running the Try/catch block. If I take this processing from Task, I lose a little in performance, the idea was to separate the queries into a normal Task and the graphical changes in Uithread, the problem is that I depend on the result of Uithread. If I take the processing of the category and product Tasks, it would be more convenient to keep everything without Task. Thank you for your attention.
– Leonan Fraga Leonardo