问题
I know, there are a lot of similar questions on SO. But I have pretty strange situation: I get this exception while it is executed inside Dispatcher.CurrentDispatcher.Invoke. I have no idea how to fix it.
Here is my StackTrace (you can see dispatcher is called there):
at System.Windows.Input.InputManager..ctor() at
System.Windows.Input.InputManager.GetCurrentInputManagerImpl() at System.Windows.Input.KeyboardNavigation..ctor() at System.Windows.FrameworkElement.FrameworkServices..ctor() at System.Windows.FrameworkElement.EnsureFrameworkServices() at System.Windows.FrameworkElement..ctor() at System.Windows.Controls.Control..ctor() at System.Windows.Controls.UserControl..ctor() at MCC.PresentationUtilities.ClosingAwareViewModelUserControl..ctor() at MCC.TherapyDeviceManager.PatientDetailUI.Views.PatientDetailView..ctor() in D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.PatientDetailUI\Views\PatientDetailView.xaml.cs:line 11 at MCC.TherapyDeviceManager.MultipleViewHost.Managers.TabItemFactory.PatientTabItemFactory.CreateEditableView(PatientDetailViewModel vm) in D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.MultipleViewHost\Managers\TabItemFactory\PatientTabItemFactory.cs:line 41 at MCC.TherapyDeviceManager.MultipleViewHost.Managers.TabItemFactory.PatientTabItemFactory.Create(SelectedPatientChangedEventArgs patient, IClosableController& controller) in D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.MultipleViewHost\Managers\TabItemFactory\PatientTabItemFactory.cs:line 31 at MCC.TherapyDeviceManager.MultipleViewHost.Managers.PatientEventsManager.CreateNewPatientTab(SelectedPatientChangedEventArgs patientInfo) in D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.MultipleViewHost\Managers\PatientEventsManager.cs:line 94 at MCC.TherapyDeviceManager.MultipleViewHost.Managers.PatientEventsManager.AddPatientView(SelectedPatientChangedEventArgs patientInfo) in D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.MultipleViewHost\Managers\PatientEventsManager.cs:line 75 at MCC.TherapyDeviceManager.MultipleViewHost.Managers.PatientEventsManager.DataImportFinishedHandler(DataImportFinishedEventArgs dataImportFinishedEventArgs) in D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.MultipleViewHost\Managers\PatientEventsManager.cs:line 41 at Microsoft.Practices.Composite.Events.EventSubscription
1.InvokeAction(Action
1 action, TPayload argument) in D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.Definitions\Prism\EventSubscription.cs:line 122 at Microsoft.Practices.Composite.Events.EventSubscription1.<>c__DisplayClass2.<GetExecutionStrategy>b__0(Object[] arguments) in D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.Definitions\Prism\EventSubscription.cs:line 108 at Microsoft.Practices.Composite.Events.EventBase.InternalPublish(Object[] arguments) in D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.Definitions\Prism\EventBase.cs:line 69 at Microsoft.Practices.Composite.Presentation.Events.CompositePresentationEvent
1.Publish(TPayload payload) in D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.Definitions\Prism\CompositePresentationEvent.cs:line 168 at MCC.TherapyDeviceManager.DeviceManager.DataRecordManager.<>c_DisplayClass5.b_4() in D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.DeviceManager\DataRecordManager.cs:line 247 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.DispatcherOperation.InvokeImpl() at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) 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.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 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Threading.DispatcherOperation.Wait(TimeSpan timeout) at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at System.Windows.Threading.Dispatcher.Invoke(Delegate method, Object[] args) at MCC.TherapyDeviceManager.DeviceManager.DataRecordManager.DataImportedIntoDb(Patient patient, DetectedDevice detectedDevice, DataRecord dataRecord) in D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.DeviceManager\DataRecordManager.cs:line 247 at MCC.TherapyDeviceManager.DeviceManager.DataRecordManager.Process(DetectedDeviceInfo detectedDeviceInfo) in D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.DeviceManager\DataRecordManager.cs:line 66 at MCC.TherapyDeviceManager.DeviceManager.DeviceToDatabaseSaverManager.CreateOrUpdateDeviceEntity(IDeviceDataInfo deviceDataInfo) in D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.DeviceManager\DeviceToDatabaseSaverManager.cs:line 106 at MCC.TherapyDeviceManager.DeviceManager.DeviceToDatabaseSaverManager.FoundDevice(IDeviceDataInfo deviceDataInfo) in D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.DeviceManager\DeviceToDatabaseSaverManager.cs:line 88 at MCC.TherapyDeviceManager.DeviceManager.DeviceToDatabaseSaverManager.DeviceDetectedHandler(Object sender, DetectedDeviceInfoEventArgs e) in D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.DeviceManager\DeviceToDatabaseSaverManager.cs:line 70 at MCC.Devices.DeviceDetection.ConnectionManagers.ConnectionManagerAggregator.OnDeviceDetected(DetectedDeviceInfoEventArgs e) in D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\ConnectionManagerAggregator.cs:line 22 at MCC.Devices.DeviceDetection.ConnectionManagers.ConnectionManagerAggregator.DeviceDetectedHandler(Object sender, DetectedDeviceInfoEventArgs e) in D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\ConnectionManagerAggregator.cs:line 58 at MCC.Devices.DeviceDetection.ConnectionManagers.SpecificConnectionManagerBase2.OnDeviceDetected(DetectedDeviceInfoEventArgs e) in D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:line 33 at MCC.Devices.DeviceDetection.ConnectionManagers.SpecificConnectionManagerBase
2.AddDevice(TConnection connection, DeviceData device) in D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:line 258 at MCC.Devices.DeviceDetection.ConnectionManagers.SpecificConnectionManagerBase2.FoundDevice(TConnection connection, DeviceData newlyDetectedDevice) in D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:line 229 at MCC.Devices.DeviceDetection.ConnectionManagers.SpecificConnectionManagerBase
2.DetectDeviceOnAquiredConnection(TConnection connection) in D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:line 185 at MCC.Devices.DeviceDetection.ConnectionManagers.SpecificConnectionManagerBase2.Poll(TConnection connection) in D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:line 166 at MCC.Devices.DeviceDetection.ConnectionManagers.Managers.SdConnectionManager.Poll(FileConnection connection) in D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\Managers\SdConnectionManager.cs:line 51 at MCC.Devices.DeviceDetection.ConnectionManagers.SpecificConnectionManagerBase
2.PollingCycle(Object tokenObject, TConnectionMetadata detectedConnectionMetadata) in D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:line 142
回答1:
If you're on a thread other than the main thread, using Dispatcher.CurrentDispatcher
will keep the code executing in your current thread, it won't move it to the main thread. And you can't create UI components (like controls) on other threads.
What you should do is to use the Dispatcher
property of some UI object. If none is accessible to you, you can use Application.Current
.
来源:https://stackoverflow.com/questions/7304472/the-calling-thread-must-be-sta-because-many-ui-components-require-this