I have tried or a while now to get my gui to show an Throbber/progress spinner while I execute a task. Unfortunately the throbber is not showing up so far. Its implemented using the MVVM principle. The button press in the Gui calls the command that then executed the method and Iam sure I need to integrate the displaying the throbber and then making the called method asynchronous. Unfortunatly the command/method call is supposed to be written as a lambda expression. And I havent found a way yet to integrate async/await into the lambda expression for this case.
//Binding of - OpenExportDataDialogCommand
private ICommand openExportDataDialogCommand;
//Here needs the activation of the Throbber to happen
//exportWindow.Throbber.IsBusy = true;
//And here the Method "ExportDataDialog" needs to be executed in a new thread, so as an async task.
public ICommand OpenExportDataDialogCommand => openExportDataDialogCommand ??
(openExportDataDialogCommand = new RelayCommand(_ => ExportDataDialog()));
private async Task ExportDataDialog()
{
//work is done and the window gets closed
}
From what I understand I would usually just "await" the Method. But I always get an syntax error no matter where I implement the await command in the lambda expression.
EDIT: I managed to improve the situation. The throbber now shows up but merely for a brief second. Then a new error hits "The calling thread cannot access this object because a different thread owns it". According to this (The calling thread cannot access this object because a different thread owns it) article I need something like
(openExportDataDialogCommand = new RelayCommand this.Dispatcher.Invoke(async _ => await ExportDataDialog());
or someone with more expierence with lambda expressions my error here is probably quiet obvious, but again i dont know how to position the Dispatcher.Invoke.