응용 프로그램이 일부 계산을 수행 할 때 표시하려는 사용 중 표시기가 있습니다.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()
많은 중복이 있지만 내 작업을 수행하지 않는다는 것을 알고 있습니다. – Mohit
스택 추적이란 무엇입니까? – SLaks