句柄无效 - 取消令牌源

时间:2016-04-25 13:25:52

标签: c# cancellation

曾几次调用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)

我无法在任何地方找到任何东西。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

问题(如评论中所述)是CancellationToken被序列化然后再次反序列化。

CancellationTokenSource(如果你直接实例化,CancellationToken创建一个)保存对非托管内核事件的引用(使用EventWaitHandle),并在内部保存对该引用处理

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

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

可能的解决方案:

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