It appears that this question has been asked multiple times before, and the suggestions are always the same... to use a dispatcher. I did, and I'm still getting that error.
var returnedValue = 0;
HasConnection = false;
ConnexionProgressBar = true;
await Task.Run(async () =>
{
returnedValue = await AddSellInvoice();
if (returnedValue != 0)
{
AddSellItems(returnedValue);
AddInstallments(returnedValue);
Dispatcher.CurrentDispatcher.Invoke(() =>
{
// Close?.Invoke(this, EventArgs.Empty);
});
}
});
Each one of these functions is running SQLLiteCommand
. I insert an invoice, get the returned value, then use that value to execute two more different queries. Everything is working fine without any issues, except the commented code above:
Close?.Invoke(this, EventArgs.Empty);
I know that because the code works fine if I don't run it but returns the following error if I do add it back:
The calling thread cannot access this object because a different thread owns it.**
Now from my understanding and from what I found online, the fix would be to use a Dispatcher, but that's not doing anything. This is the command that opens the dialog, and also what's being triggered whenever Close?.Invoke
is called:
await Dispatcher.CurrentDispatcher.Invoke(async () =>
{
var createVenteViewModel = new CreateDialogs.CreateVenteViewModel();
var createVenteView = new CreateDialogs.CreateVente(createVenteViewModel);
await DialogHost.Show(createVenteView, "MainDialog", (object sender, DialogOpenedEventArgs e) =>
{
void OnClose(object _, EventArgs args)
{
createVenteViewModel.Close -= OnClose;
e.Session.Close();
// ListeVente.Clear();
// ListeVente = GetListeVente();
// _snackbarMessageQueue.Enqueue("La vente a été ajouté avec succée.");
//RefreshCommandExecute();
}
createVenteViewModel.Close += OnClose;
});
});