Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Rx连接受限制的TextBox.TextChanged与TextBox.GotFocus/(延迟)LostFocus会引发System.OperationCanceledException_C#_Wpf_System.reactive - Fatal编程技术网

C# 使用Rx连接受限制的TextBox.TextChanged与TextBox.GotFocus/(延迟)LostFocus会引发System.OperationCanceledException

C# 使用Rx连接受限制的TextBox.TextChanged与TextBox.GotFocus/(延迟)LostFocus会引发System.OperationCanceledException,c#,wpf,system.reactive,C#,Wpf,System.reactive,我正在使用Rx为WPF中的文本框创建som类型的“ProbablyOneTyping” 通过将GotFocus中的一个Observable与TextChanged中的一个Observable连接起来,我让所有的事情都能正常工作 var gotFocus = Observable.FromEventPattern<RoutedEventArgs>(textBox, "GotFocus"); var delayedLostFocus = Observable.FromEventPatte

我正在使用Rx为WPF中的文本框创建som类型的“ProbablyOneTyping”

通过将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);
var gotFocus=Observable.FromEventPattern(文本框,“gotFocus”);
var delayedLostFocus=可观察的。FromEventPattern(文本框,“LostFocus”)
.延迟(时间跨度从毫秒(850));
var throttledTextChanged=Observable.FromEventPattern(文本框,“TextChanged”)
.Select(pattern=>textBox.Text).Throttle(TimeSpan.frommillistes(750)).DistinctUntilChanged();
var立即=可观察的.Empty();
var probablyDoneTyping=gotFocus.Join(throttledTextChanged,=>delayedLostFocus,=>instally,(u,text)=>text);
probablyDoneTyping.ObserveOn(SynchronizationContext.Current).Subscribe(text=>Title=text);
我在LostFocus上引入了延迟,这样我就可以离开文本框了,而且可能的话,打字功能仍然会启动

但这给了我一个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()     
System.Reactive.Linq.dll!System.Reactive.Linq.ObservableImpl.Delay.LongRunningImpl.DrainQueue正常
mscorlib.dll!System.Threading.CancellationToken.ThrowoOperationCanceledException()
mscorlib.dll!System.Threading.SemaphoreSlim.Wait(int毫秒计时,System.Threading.CancellationToken CancellationToken)
mscorlib.dll!System.Threading.SemaphoreSlim.Wait(System.Threading.CancellationToken CancellationToken)
System.Reactive.Linq.dll!System.Reactive.Linq.ObservableImpl.Delay.LongRunningImpl.DrainQueue(System.Reactive.Disposables.ICancelable cancel)
System.Reactive.Core.dll!System.Reactive.Concurrency.Scheduler.ScheduleLongRunning.AnonymousMethod_uu72(System.Action a,System.Reactive.Disposables.ICANCellable c)
System.Reactive.Core.dll!System.Responsive.Concurrency.DefaultScheduler.LongRunning.ScheduleLongRunning.AnonymousMethod_u__b(对象参数)
System.Reactive.PlatformServices.dll!System.Reactive.Concurrency.concurrencyAbstrationLayerImpl.StartThread.AnonymousMethod__4()
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart\u上下文(对象状态)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext ExecutionContext,System.Threading.ContextCallback回调,对象状态,bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext ExecutionContext,System.Threading.ContextCallback回调,对象状态,bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext ExecutionContext,System.Threading.ContextCallback回调,对象状态)
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()
异常不会影响我的应用程序,但会引发异常,如果我因公共语言运行时异常而中断,则每次我离开文本框时,应用程序都会停止。我不要这个


为什么会引发异常以及如何消除它?

启用或禁用(尽管我不推荐后者)。

从下面的源代码中,延迟方法应该捕获异常。您使用的是什么版本的Rx?我使用的是版本2.2.5Ok,我可以从源代码中看到异常被捕获。我能从中得出的结论是,这是人们期望的行为,并且会爱上它。