在线程上进行用户身份验证时显示忙指示符:线程错误(调用线程无法访问此对象)
本文关键字:线程 调用 错误 访问 对象 指示符 用户 身份验证 显示 | 更新日期: 2023-09-27 18:20:07
我正在开发一个WPF应用程序,并遵循MVVM方法。当用户在进行身份验证时点击"输入"按钮时,我必须在登录屏幕上显示忙碌指示灯。在"Enter"按钮上,我有一个名为"EnterCommand"的ICommand,然后检查身份验证,然后在成功身份验证时加载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属性绑定到此showprogress
private bool _ShowProgress = false;
public bool ShowProgress
{
get { return _ShowProgress; }
set
{
if (_ShowProgress != value)
{
_ShowProgress = value;
FirePropertyChanged("ShowProgress");
}
}
}
我正在这个命令上创建一个线程,然后从(bool Property name : ShowProgress)
MVVM设置IsBusy属性。
在LoadApplication:中
public void LoadApplication()
{
ShowProgress= true;
if (AuthenticateUser)
{
MainWindow objMainWindow = new MainWindow();
objMainWindow.Show();
Application.Current.MainWindow.Close();
}
ShowProgress= false;
}
Error: objMainWindow.Show()
抛出错误-调用线程无法访问此对象,因为其他线程拥有它。
同样在App.xaml中,我已经将StartupUri设置为我的"登录"窗口。
当用户单击"输入"按钮时,它可以显示忙碌指示器,但在显示主窗口时失败。
只要我的MainWindow
(主屏幕)没有启动,我就必须显示busyindicator
。
Error: objMainWindow.Show() in LoadApplication()
抛出错误-调用线程无法访问此对象,因为其他线程拥有它。
Stack Trace:
at System.Windows.Markup.XamlReader.RewrapException(Exception e, Uri baseUri)
at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)
at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)
at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)
at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)
at System.Windows.FrameworkElement.ApplyTemplate()
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.StackPanel.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Control.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Documents.AdornerDecorator.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Border.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Window.MeasureOverrideHelper(Size constraint)
at System.Windows.Window.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Interop.HwndSource.SetLayoutSize()
at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
at System.Windows.Window.SetRootVisual()
at System.Windows.Window.SetRootVisualAndUpdateSTC()
at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
at System.Windows.Window.CreateSourceWindowDuringShow()
at System.Windows.Window.SafeCreateWindowDuringShow()
at System.Windows.Window.ShowHelper(Object booleanBox)
at System.Windows.Window.Show()
at RBS.MIB.DS.Reporting.UI.MainViewModelStartupWindow.LoadApplication() in C:'DSReporting'trunk'Projects'csharp'UI'RBS.MIB.DS.Reporting.UI'StartUpWindow'StartupViewModel'MainViewModelStartupWindow.cs:line 104
InnerException: System.InvalidOperationException
Message=The calling thread cannot access this object because a different thread owns it.
请尝试以下操作:
private BackgroundWorker _BgWorker;
public LoginFormViewModel()
{
_BgWorker = new BackgroundWorker();
_BgWorker.DoWork += new DoWorkEventHandler(bgw_DoWork);
_BgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
_BgWorker.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
_BgWorker.RunWorkerAsync();
}
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// write your code to check authentication
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
// write you code to open window
}
void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// write the code for busy indicator like
ShowProgress= true;
}
希望这能对你有所帮助。