I know there are similar questions like Here And Here, I looked them all, but they seems not work for me.
I have a thread:
private void Sample()
{
Thread t = new Thread(new ThreadStart(Sample_Thread));
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
In the Sample_Thread, I previously called MessageBox
which works fine.
private void Sample_Thread()
{
try{ ... }
catch(Exception e)
{
MessageBox.Show("SampleText");
}
}
Now I try to call ModernDialog
instead of MessageBox
, it gives me error 'The calling thread cannot access this object because a different thread owns it.'
so I change my code to this:
private void Sample_Thread()
{
try{ ... }
catch(Exception e)
{
Dispatcher.CurrentDispatcher.Invoke(() =>
{
ModernDialog.ShowMessage("SampleText");
});
}
}
But this still got that same error, how should I fix this? Thanks!