Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

c# - displaying Busy Indicator on a thread and launching the application : Threading error (The calling thread cannot access this object)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

the exception is thrown because you cannot access UI element form a background thread.

you have to either call the objMainWindow.Show() from main/UI thread or ask the dispatcher to call it for you

if you have to call it from the backgorund thread you can do it like this:

Action act = () =>
{
   MainWindow objMainWindow = new MainWindow();
   objMainWindow.Show();
  Application.Current.MainWindow.Close();
};

 System.Windows.Application.Current.Dispatcher.Invoke(act);

I would recomend reading the following MSDN Article


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...