2012-12-04 2 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(); 

하지만 응용 프로그램을 실행할 때 그것은 "{다른 스레드가 그것을 소유하고 있기 때문에 호출 스레드가이 개체를 에 액세스 할 수 없습니다.}"

I를

System.InvalidOperationException를 던졌습니다 이 문제를 해결하는 방법을 알 수 없습니다.

을 제거하면 모든 것이 제대로 작동하지만 소유자가 올바르게 설정되지 않아서 응용 프로그램의 크기가 다시 조정되지 않습니다. 응용 프로그램을 최소화하면 맨 위에 놓여 있습니다.

스택 추적 : 당신의 배경 스레드에서

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

관련 문제