简体   繁体   English

等待“最后”事件触发

[英]Wait for “last” event to fire

I need to block execution of my code until a user enters something into a TextBox . 我需要阻止代码执行,直到用户在TextBox输入内容为止。

However, in this particular case, I actually need to wait "until the user has finished typing". 但是,在这种特殊情况下,我实际上需要等待“直到用户完成键入”。 Ie, I don't want to just wait for the first time that the TextChanged event fires, I want to wait until at least 500ms have passed since the last time it fired before continuing the execution of my method. 即,我不想仅等待TextChanged事件第一次触发,而是要等待自上次触发以来至少经过500毫秒,然后再继续执行我的方法。

Currently I have something that uses timers where every time the TextChanged event fires the timer is restarted and when the timer's Tick event fires it means that the user "has finished typing". 当前,我使用计时器,每次TextChanged事件触发时,计时器都会重新启动,而计时器的Tick事件触发时,则意味着用户“已完成键入”。

There are three main problems with this approach right now though: 但是,此方法目前存在三个主要问题:

  • There has to be a better/cleaner way than using a Timer for this 为此,必须有一个比使用Timer更好/更干净的方法
  • Up until now I did not need to block execution until the user "has finished typing", so was basically just adding my code to the Tick event handler, that's not changed though. 到现在为止,我不需要阻塞执行,直到用户“完成键入”为止,因此基本上只是将我的代码添加到Tick事件处理程序中,尽管没有改变。
  • I need to set a timeout as well, so that if the user hasn't finished typing in a certain amount of time, it should just return back to the method as having failed to wait for the user. 我还需要设置一个超时时间,这样,如果用户在一定时间内还没有完成输入操作,它就应该因为无法等待用户而返回到该方法。

How can I accomplish this? 我该怎么做?

This is very easy to do using ReactiveUI . 使用ReactiveUI非常容易做到这一点。 Look at the example on the main page. 查看主页上的示例。 It does more or less exactly what you are after. 它或多或少地精确地满足了您的需求。

It is based on Reactive Extensions (RX) for .Net and is written to make things like these easy. 它基于.Net的Reactive Extensions(RX),旨在简化此类操作。 If using ReactiveUI is not an option then I suggest you look at RX and how ReactiveUI implements this. 如果不选择使用ReactiveUI,则建议您查看RX以及ReactiveUI如何实现这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM