gpt4 book ai didi

c# - 使用 Rx 将节流的 TextBox.TextChanged 与 TextBox.GotFocus/(delayed)LostFocus 结合起来会抛出 System.OperationCanceledException

转载 作者:太空宇宙 更新时间:2023-11-03 15:51:40 25 4
gpt4 key购买 nike

我正在使用 Rx 为 WPF 中的文本框创建某种“ProbablyDoneTyping”。

我通过像这样将来自 GotFocus 的 Observable 与来自 TextChanged 的​​ Observable 连接起来,让一切正常工作

var gotFocus = Observable.FromEventPattern<RoutedEventArgs>(textBox, "GotFocus");
var delayedLostFocus = Observable.FromEventPattern<RoutedEventArgs>(textBox, "LostFocus")
.Delay(TimeSpan.FromMilliseconds(850));

var throttledTextChanged = Observable.FromEventPattern<TextChangedEventArgs>(textBox, "TextChanged")
.Select(pattern => textBox.Text).Throttle(TimeSpan.FromMilliseconds(750)).DistinctUntilChanged();
var immediately = Observable.Empty<Unit>();

var probablyDoneTyping = gotFocus.Join(throttledTextChanged, _ => delayedLostFocus, _ => immediately, (_, text) => text);
probablyDoneTyping.ObserveOn(SynchronizationContext.Current).Subscribe(text => Title = text);

我在 LostFocus 上引入了延迟,这样我就可以离开 TextBox 并且 probablyDoneTyping 仍然会触发。

但这给了我一个 System.OperationCanceledException,每次我离开文本框时,它都会在延迟中产生。

异常的堆栈跟踪是

System.Reactive.Linq.dll!System.Reactive.Linq.ObservableImpl.Delay<System.Reactive.EventPattern<System.Windows.RoutedEventArgs>>.LongRunningImpl.DrainQueue Normal
mscorlib.dll!System.Threading.CancellationToken.ThrowOperationCanceledException()
mscorlib.dll!System.Threading.SemaphoreSlim.Wait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken)
mscorlib.dll!System.Threading.SemaphoreSlim.Wait(System.Threading.CancellationToken cancellationToken)
System.Reactive.Linq.dll!System.Reactive.Linq.ObservableImpl.Delay<System.Reactive.EventPattern<System.Windows.RoutedEventArgs>>.LongRunningImpl.DrainQueue(System.Reactive.Disposables.ICancelable cancel)
System.Reactive.Core.dll!System.Reactive.Concurrency.Scheduler.ScheduleLongRunning.AnonymousMethod__72(System.Action<System.Reactive.Disposables.ICancelable> a, System.Reactive.Disposables.ICancelable c)
System.Reactive.Core.dll!System.Reactive.Concurrency.DefaultScheduler.LongRunning.ScheduleLongRunning<System.Action<System.Reactive.Disposables.ICancelable>>.AnonymousMethod__b(object arg)
System.Reactive.PlatformServices.dll!System.Reactive.Concurrency.ConcurrencyAbstractionLayerImpl.StartThread.AnonymousMethod__4()
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()

异常不会影响我的应用程序,但它会被抛出,如果我因公共(public)语言运行时异常而中断,每次我离开文本框时应用程序都会停止。我不想要这个。

为什么会抛出异常,我该如何摆脱它?

最佳答案

启用Just My Code或禁用 breaking on first-chance exceptions (虽然我不推荐后者)。

关于c# - 使用 Rx 将节流的 TextBox.TextChanged 与 TextBox.GotFocus/(delayed)LostFocus 结合起来会抛出 System.OperationCanceledException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25523014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com