调用线程必须是STA,因为很多UI组件都需要这个
我知道,SO上有很多类似的问题。但我遇到了非常奇怪的情况:在 Dispatcher.CurrentDispatcher.Invoke 内执行时出现此异常。我不知道如何解决它。
这是我的 StackTrace(您可以看到调度程序在那里被调用):
System.Windows.Input.InputManager..ctor() 处的在
System.Windows.Input.InputManager.GetCurrentInputManagerImpl() 在 System.Windows.Input.KeyboardNavigation..ctor() 在 System.Windows.FrameworkElement.FrameworkServices..ctor() 在 System.Windows.FrameworkElement.EnsureFrameworkServices() 在 System.Windows.FrameworkElement..ctor() 在 System.Windows.Controls.Control..ctor() 在 System.Windows.Controls.UserControl..ctor() 在 MCC.PresentationUtilities.ClosingAwareViewModelUserControl..ctor() 在 MCC.TherapyDeviceManager.PatientDetailUI.Views.PatientDetailView..ctor() 在 D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.PatientDetailUI\Views\PatientDetailView.xaml.cs:行 11 在 MCC.TherapyDeviceManager.MultipleViewHost.Managers.TabItemFactory.PatientTabItemFactory.CreateEditableView(PatientDetailViewModel 虚拟机)在 D:\ WTS \ WTS3 \ WTS \ MCC.TherapyDeviceManager.MultipleViewHost \ Managers \ TabItemFactory \ PatientTabItemFactory.cs:行 41 在 MCC.TherapyDeviceManager.MultipleViewHost.Managers.TabItemFactory.PatientTabItemFactory.Create(SelectedPatientChangedEventArgs 病人,IClosableController&控制器)中 D:\ WTS \ WTS3 \ WTS \ MCC.TherapyDeviceManager.MultipleViewHost \ Managers \ TabItemFactory \ PatientTabItemFactory.cs:行 31 在 MCC.TherapyDeviceManager.MultipleViewHost.Managers.PatientEventsManager.CreateNewPatientTab(SelectedPatientChangedEventArgs 患者信息)中 D:\ WTS \ WTS3 \ WTS \ MCC.TherapyDeviceManager.MultipleViewHost \ Managers \ PatientEventsManager.cs:行 94 在 MCC.TherapyDeviceManager.MultipleViewHost.Managers.PatientEventsManager.AddPatientView(SelectedPatientChangedEventArgs 患者信息)中 D:\ WTS \ WTS3 \ WTS \ MCC.TherapyDeviceManager.MultipleViewHost \ Managers \ PatientEventsManager.cs:行 75 在 MCC.TherapyDeviceManager.MultipleViewHost.Managers.PatientEventsManager.DataImportFinishedHandler(DataImportFinishedEventArgs dataImportFinishedEventArgs) 中 D:\ WTS \ WTS3 \ WTS \ MCC.TherapyDeviceManager.MultipleViewHost \ Managers \ PatientEventsManager.cs:行 41 在 Microsoft.Practices.Composite.Events.EventSubscription
1.InvokeAction(Action
1 动作,TPayload 参数) D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.Definitions\Prism\EventSubscription.cs:line 122 在 Microsoft.Practices.Composite.Events.EventSubscription1.<>c__DisplayClass2.
1.Publish(TPayload 有效负载)在 D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.Definitions\Prism\CompositePresentationEvent.cs:行 168 在 MCC.TherapyDeviceManager.DeviceManager.DataRecordManager.<>c_DisplayClass5.b_4() 在 D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.DeviceManager\DataRecordManager.cs:行 第247章 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托 回调、对象参数、Int32 numArgs) 在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象 源、委托方法、对象参数、Int32 numArgs、委托 捕获处理程序) 在 System.Windows.Threading.DispatcherOperation.InvokeImpl() 在 System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象 状态) 在 System.Threading.ExecutionContext.runTryCode(对象 用户数据) 在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode 代码、CleanupCode backoutCode、对象 userData) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext、ContextCallback 回调、对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 忽略SyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态) 在 System.Windows.Threading.DispatcherOperation.Invoke() 在 System.Windows.Threading.Dispatcher.ProcessQueue() 在 System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg、IntPtr wParam、IntPtr lParam、Boolean&已处理) 在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam、IntPtr lParam、布尔值&已处理) 在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象 o) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托 回调、对象参数、Int32 numArgs) 在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象 源、委托方法、对象参数、Int32 numArgs、委托 捕获处理程序) 在 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority 优先级、TimeSpan 超时、委托方法、对象参数、Int32 参数数量) 在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg、IntPtr wParam、IntPtr lParam) 在 MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame 框架) 在 System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame框架) 在 System.Windows.Threading.DispatcherOperation.Wait(TimeSpan 暂停) 在 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority 优先级、TimeSpan 超时、委托方法、对象参数、Int32 参数数量) 在 System.Windows.Threading.Dispatcher.Invoke(委托方法, 对象[] 参数) 在 MCC.TherapyDeviceManager.DeviceManager.DataRecordManager.DataImportedIntoDb(患者 患者、DetectedDevice(检测到的设备)、DataRecord(数据记录)中 D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.DeviceManager\DataRecordManager.cs:行 第247章 在 MCC.TherapyDeviceManager.DeviceManager.DataRecordManager.Process(DetectedDeviceInfo 检测到的设备信息)中 D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.DeviceManager\DataRecordManager.cs:行 66 在 MCC.TherapyDeviceManager.DeviceManager.DeviceToDatabaseSaverManager.CreateOrUpdateDeviceEntity(IDeviceDataInfo 设备数据信息)中 D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.DeviceManager\DeviceToDatabaseSaverManager.cs:行 106 在 MCC.TherapyDeviceManager.DeviceManager.DeviceToDatabaseSaverManager.FoundDevice(IDeviceDataInfo 设备数据信息)中 D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.DeviceManager\DeviceToDatabaseSaverManager.cs:line 88 在 MCC.TherapyDeviceManager.DeviceManager.DeviceToDatabaseSaverManager.DeviceDetectedHandler(对象 发送者、DetectedDeviceInfoEventArgs e) 中 D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.DeviceManager\DeviceToDatabaseSaverManager.cs:行 70 在 MCC.Devices.DeviceDetection.ConnectionManagers.ConnectionManagerAggregator.OnDeviceDetected(DetectedDeviceInfoEventArgs e) 在 D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\ConnectionManagerAggregator.cs:行 22 号 在 MCC.Devices.DeviceDetection.ConnectionManagers.ConnectionManagerAggregator.DeviceDetectedHandler(对象 发送者、DetectedDeviceInfoEventArgs e) 中 D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\ConnectionManagerAggregator.cs:行 58 在 MCC.Devices.DeviceDetection.ConnectionManagers.SpecificConnectionManagerBaseb__0(Object[] 参数)中 D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.Definitions\Prism\EventSubscription.cs:行 108 在 Microsoft.Practices.Composite.Events.EventBase.InternalPublish(对象[] 参数)中 D:\WTS\WTS3\WTS\MCC.TherapyDeviceManager.Definitions\Prism\EventBase.cs:行 69 在 Microsoft.Practices.Composite.Presentation.Events.CompositePresentationEvent 2.OnDeviceDetected(DetectedDeviceInfoEventArgs e) 在 D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:行 33 在 MCC.Devices.DeviceDetection.ConnectionManagers.SpecificConnectionManagerBase
2.AddDevice(TConnection 连接、DeviceData 设备)中 D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:行 258 在 MCC.Devices.DeviceDetection.ConnectionManagers.SpecificConnectionManagerBase2.FoundDevice(TConnection 连接,DeviceData newDetectedDevice) 中 D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:行 229 在 MCC.Devices.DeviceDetection.ConnectionManagers.SpecificConnectionManagerBase
2.DetectDeviceOnAquiredConnection(TConnection 连接)中 D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:行 185 在 MCC.Devices.DeviceDetection.ConnectionManagers.SpecificConnectionManagerBase2.Poll(TConnection 连接)中 D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:行 166 在 MCC.Devices.DeviceDetection.ConnectionManagers.Managers.SdConnectionManager.Poll(FileConnection 连接)中 D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\Managers\SdConnectionManager.cs:行 51 在 MCC.Devices.DeviceDetection.ConnectionManagers.SpecificConnectionManagerBase
2.PollingCycle(对象 tokenObject、TConnectionMetadata 检测到的ConnectionMetadata) 中 D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:行 142
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.EventSubscription1.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[]
1.Publish(TPayload
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
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
2.AddDevice(TConnection
e) in
D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:line
33
at
MCC.Devices.DeviceDetection.ConnectionManagers.SpecificConnectionManagerBase
connection, DeviceData device) in
D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:line
258
at
MCC.Devices.DeviceDetection.ConnectionManagers.SpecificConnectionManagerBase2.FoundDevice(TConnection
2.DetectDeviceOnAquiredConnection(TConnection
connection, DeviceData newlyDetectedDevice) in
D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:line
229
at
MCC.Devices.DeviceDetection.ConnectionManagers.SpecificConnectionManagerBase
connection) in
D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:line
185
at
MCC.Devices.DeviceDetection.ConnectionManagers.SpecificConnectionManagerBase2.Poll(TConnection
2.PollingCycle(Object
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
tokenObject, TConnectionMetadata detectedConnectionMetadata) in
D:\WTS\WTS3\WTS\MCC.Devices.DetectService\ConnectionManagers\SpecificConnectionManagerBase.cs:line
142
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您位于主线程以外的线程上,则使用 Dispatcher.CurrentDispatcher 将使代码保持在当前线程中执行,而不会将其移动到主线程。并且您无法在其他线程上创建 UI 组件(如控件)。
您应该做的是使用某些 UI 对象的 Dispatcher 属性。如果您无法访问任何内容,则可以使用
Application.Current
。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 useApplication.Current
.