2012-12-04 68 views
0

我有一個繁忙的指標,當我的應用程序正在做一些計算時,我想顯示。System.InvalidOperationException調用線程不能訪問這個對象,因爲不同的線程擁有它

var uiThread = new Thread(() => 
    { 
     autoResetEvent.Set(); 
     bussyWindowVM.Dispatcher = Dispatcher.CurrentDispatcher; 
     Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate 
      { 
       var busyWindow = new BusyWindow 
       { 
        DataContext = bussyWindowVM, 
        Owner = Application.Current.MainWindow, 
        WindowStartupLocation = WindowStartupLocation.CenterOwner 
       }; 
       busyWindow.Show(); 
      }); 

     Dispatcher.Run(); 
    }); 
// set single threaded apartment 
uiThread.SetApartmentState(ApartmentState.STA); 

// mark UI thread as background thread 
uiThread.IsBackground = false; 

// start the UI thread 
uiThread.Start(); 

// wait until thread exits 
autoResetEvent.WaitOne(); 

但是當我運行應用程序,它拋出

System.InvalidOperationException 「{因爲不同的線程擁有它調用線程不能訪問此 對象。}」

我我無法弄清楚如何解決這個問題。

但是,如果我刪除Owner = Application.Current.MainWindow然後一切工作正常,但業主沒有正確設置,所以我重新調整我的應用程序的窗口不應該中心的應用程序也保持最前面,當我最小化應用程序。

堆棧跟蹤:

at System.Windows.Threading.DispatcherObject.VerifyAccess() 
    at System.Windows.Application.get_MainWindow() 
    at MyProject.MyViewModel.<>c__DisplayClass3.<CreateBusyWindow>b__2() in D:\MyStuff\Dev\Repo\MyProject\ViewModels\MyViewModel.cs:line 2482 
    at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
    at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
    at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) 
    at System.Windows.Threading.DispatcherOperation.InvokeImpl() 
    at System.Threading.ExecutionContext.runTryCode(Object userData) 
    at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Windows.Threading.DispatcherOperation.Invoke() 
    at System.Windows.Threading.Dispatcher.ProcessQueue() 
    at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
    at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
    at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 
    at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
    at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
    at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) 
    at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) 
    at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
    at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 
    at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 
    at CivilGeo.GeoHECRAS.ViewModels.GeoHECRASViewModel.<>c__DisplayClass3.<CreateBusyWindow>b__1() in D:\MyStuff\Dev\CivilGeoRepo\GeoHECRAS\CivilGeo.GeoHECRAS\ViewModels\GeoHECRASViewModel.cs:line 2492 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ThreadHelper.ThreadStart() 
+0

我知道有很多重複存在,但不要做我的工作 – Mohit

+0

什麼是堆棧跟蹤? – SLaks

回答

0

在你的後臺線程Dispatcher.CurrentDispatcher創造了新的Dispatcher該線程的一個品牌。
該調度程序無法訪問原始UI線程中的對象,如Application.Current.MainWindow

如果你想運行一個單獨的UI線程,你將無法設置Owner。另外,您不應該撥打BeginInvoke()

+0

我應該調用什麼而不是BeginInvoke()。此外,我想設置所有者,因爲它必須來中心應用程序,並儘量減少文字應用程序。 – Mohit

+0

@Mohit:您無法將所有者設置爲不同UI線程上的窗口。基本上,不要這樣做。 – SLaks

相關問題