I'm developing a WPF application and following MVVM approach. I have to show the busy indicator on my log-in screen when an user click on the 'Enter' Button. On 'Enter' button I am having an ICommand named 'EnterCommand' which then check the authentication and then loads the MainWindow.
private ICommand _EnterCommand;
public ICommand EnterCommand
{
get
{
return _EnterCommand ?? (_EnterCommand = new DelegateCommand(() =>
{
Thread objThread = new Thread(LoadApplication);
objThread.SetApartmentState(ApartmentState.STA);
objThread.Start();
}));
}
}
IsBusy propery is bound to this showprogress
private bool _ShowProgress = false;
public bool ShowProgress
{
get { return _ShowProgress; }
set
{
if (_ShowProgress != value)
{
_ShowProgress = value;
FirePropertyChanged("ShowProgress");
}
}
}
I'm creating a thread on this command then setting the IsBusy property from (bool Property name : ShowProgress) MVVM.
in LoadApplication:
public void LoadApplication()
{
ShowProgress= true;
if (AuthenticateUser)
{
MainWindow objMainWindow = new MainWindow();
objMainWindow.Show();
Application.Current.MainWindow.Close();
}
ShowProgress= false;
}
objMainWindow.Show() is throwing error that - The calling thread cannot access this object because a different thread owns it.
Also in App.xaml I have set StartupUri as my 'Log-in' window.
This is able to show the Busy Indicator as soon as user clicks on the 'Enter' button however failed while showing the mainWindow.
Error message is "The calling thread cannot access this object because a different thread owns it."
I have to show the busyindicator as long as my MainWindow (which is home screen) is not launched.
Any help to rectify this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…