Je dois créer une fenêtre avec le chargement de gif lorsque ma fenêtre principale est en cours de rendu. J'ai lu quelques articles et j'ai pris la décision de créer un nouveau fil à cette fin. Je l'ai fait comme dans this articleWPF Fenêtre de loisirs dans un thread STA différent
En conséquence, j'ai quelque chose comme ça:
LoadingDialog _loadingDlg;
Thread loadingThread;
public void ShowLoading()
{
loadingThread = new Thread(new ThreadStart(loadingThreadWork));
loadingThread.SetApartmentState(ApartmentState.STA);
loadingThread.Start();
}
private void loadingThreadWork()
{
_loadingDlg = new LoadingDialog();
_loadingDlg.Show();
System.Windows.Threading.Dispatcher.Run();
}
public void HideLoading()
{
_loadingDlg.Dispatcher.InvokeShutdown();
}
première fois quand je l'appelle ShowLoading()
puis HideLoading()
tout fonctionne comme je veux. Mais quand je l'appelle ShowLoading()
à la deuxième fois que je reçois une exception à
_loadingDlg.Show();
avec le message The calling thread cannot access this object because a different thread owns it
.
Comment cela peut-il être? _loadingDlg
est créé dans la ligne précédente et dans le même fil.
Pourquoi créer une nouvelle fenêtre? Pourquoi ne pas simplement faire une grille centrée, avec une visibilité que vous basculez? –