【发布时间】:2016-04-25 13:25:51
【问题描述】:
曾几何时,当调用 CancellationTokenSource Cancel() 方法时,我遇到了一个异常:The handle is invalid。
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.Threading.EventWaitHandle.Set()
at System.Threading.CancellationTokenSource.NotifyCancellation(Boolean throwOnFirstException)
at System.Threading.CancellationTokenSource.Cancel()
at .CancelProcess(Object obj) in proj\ViewModels\WorkflowContainerViewModel.cs:line 162
at DelegateCommand.Execute(Object parameter) in proj\Commands\DelegateCommand.cs:line 32
at MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(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 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
我在任何地方都找不到任何东西。有什么想法吗?
【问题讨论】:
-
想要关闭它的人-请告诉我是什么原因
-
看到那个堆栈跟踪,我能够找到这个问题的解决方案,并且还在您的代码中发现了十五个其他错误,三个在 .NET 框架中,四个在 WPF 中。
-
酷,我想不出其他重要的东西可以在这里展示。我正在调用 cts.Cancel() ,这是堆栈跟踪。你要我展示什么?您可以从堆栈跟踪中看到它是从绑定到命令的 wpf 控件调用的,但我不明白为什么它很重要。
-
Zbignew,
invalid handle问题表明,当您尝试对已处理内核事件(并且其句柄丢失)的CancellationTokenSource执行某些操作时。这在正常使用时不应该发生,但是如果您正在序列化和反序列化它(原始对象丢失并且内核事件被处理,反序列化后它会尝试设置相同的本机句柄,但是那个不存在)。发生这种情况的原因有几个:如果没有最小的可复制示例,根本不可能帮助您 -
在问题中提到比堆栈跟踪更有效率:-) 如果您找到原因,请告诉我们
标签: c# cancellation