【问题标题】:The handle is invalid - Cancellation token source [closed]句柄无效-取消令牌源[关闭]
【发布时间】: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


【解决方案1】:

问题(如 cmets 所示)是 CancellationToken 被序列化,然后再次反序列化。

CancellationTokenSource(如果您直接实例化它,CancellationToken 会创建一个)持有对非托管内核事件的引用(使用 EventWaitHandle),并在内部持有对该句柄的引用。

CancellationTokenSource 被释放时,它会释放该非托管引用...因此,如果您序列化令牌,它将使用该句柄进行序列化...之后,您不再使用CancellationTokenSource所以垃圾收集器收集它,并释放内核事件(因此句柄不再有效)。

现在在反序列化时,EventWaitHandle 会取回值,但它不会再次创建内核事件(因为在反序列化时,您只是将值分配给字段,而不是再次执行构造逻辑),但它保留了旧句柄......所以当你想取消它时,它会尝试使用不再有效的内核事件句柄:因此,你的异常。

可能的解决方案:

  1. 不要序列化/反序列化 CancellationToken... 而是在反序列化时构造一个不同的。
  2. 如果这不可能(因为您需要使用那个,而不能使用另一个),那么请在某处保留对 CancellationTokenSource 的引用,这样它就不会被垃圾收集,并且它永远不会释放内核事件,所以反序列化时句柄仍然有效

【讨论】:

    猜你喜欢
    • 2018-06-20
    • 2019-04-21
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 2019-12-24
    相关资源
    最近更新 更多