c# - C# 中 KeyUp 事件处理程序的速率限制

标签 c#

我想在用户键入时验证文本字段中的输入。此功能工作正常,但我想对验证进行速率限制,因为它正在访问外部 API。我只想在用户 750 毫秒未输入内容后执行验证。

ATM 我只是使用这个:

private void Configure_Load(object sender, EventArgs e)
{
    endpointBox.KeyUp += EndpointBox_KeyUp;
}

void EndpointBox_KeyUp(object sender, KeyEventArgs e)
{
    TestHTTP200(endpointBox.Text);
}

最佳答案

使用 Timer控制

System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
private void Configure_Load(object sender, EventArgs e)
{
    endpointBox.KeyUp += EndpointBox_KeyUp;
    myTimer.Tick +=new EventHandler(OnTimedEvent);       //EDIT: should not be `ElapsedEventHandler`  
    myTimer.Interval=750;   
}

void EndpointBox_KeyUp(object sender, KeyEventArgs e)
{    
    myTimer.Stop();
    myTimer.Start();   
}


 private void OnTimedEvent(Object myObject,EventArgs myEventArgs) 
{
      myTimer.Stop();
      TestHTTP200(endpointBox.Text);
}

关于c# - C# 中 KeyUp 事件处理程序的速率限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16507326/

相关文章:

c# - ASP :NET MVC: How can I render two styles in one page without any overlaps of CSS classes?

c# - Unity 不拦截 WCF 服务调用

c# - 如何继承构造函数?

c# - 为什么构造函数中不允许类型参数?

c# - 如何用javascript按下按钮

c# - .NET 中的线程池和 .IsBackground

c# - 有没有办法注入(inject)/交换扩展?

c# - 将 VB.net 替换转换为 C#

c# - 如何在不破坏现有客户端的情况下使用 C# 重新实现旧的 DCOM 服务器?

c# - 分两步将复选框值从 View 发送到 Controller ?