2013-05-12 30 views
2

我想驗證文本字段上的輸入作爲用戶類型。這個功能可以正常工作,但是我想限制驗證,因爲它正在打外部API。我想只在用戶輸入750毫秒後執行驗證。我只是用這個C#中KeyUp事件處理程序的速率限制

ATM:

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

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

設置一個['Timer'](http://msdn.microsoft.com/en-AU/library /system.timers.timer(v=vs.110).aspx)放在'KeyUp'上,然後將事件處理程序Timer.Elapsed設置爲API。 – 2013-05-12 12:33:41

+0

我認爲[rx](http://msdn.microsoft.com/zh-cn/data/gg577609.aspx)對此具有很好的功能 – 2013-05-12 12:49:48

回答

1

使用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); 
} 
+0

這看起來像一個整潔的解決方案,但我得到這個編譯:'無法隱式轉換類型「System.Timers.ElapsedEventHandler」到「System.EventHandler' – SimonJGreen 2013-05-12 12:50:33

+0

遺憾應該是'EventHandler'不'ElapsedEventHandler'編輯 – Bolu 2013-05-12 12:54:22

+0

和它完美的作品:)謝謝! – SimonJGreen 2013-05-12 12:55:35

1

您想等於JavaScript的setTimeout的方法等。當用戶提供更多輸入時,這可以被取消:

代碼取自here

public static IDisposable SetTimeout(Action method, int delayInMilliseconds) 
    { 
     System.Timers.Timer timer = new System.Timers.Timer(delayInMilliseconds); 
     timer.Elapsed += (source, e) => 
     { 
      method(); 
     }; 

     timer.AutoReset = false; 
     timer.Enabled = true; 
     timer.Start(); 

     // Returns a stop handle which can be used for stopping 
     // the timer, if required 
     return timer as IDisposable; 
    } 

然後你可以在你的鑰匙了處理程序使用此:

void EndpointBox_KeyUp(object sender, KeyEventArgs e) 
    { 
     IDisposable timeout = SetTimeout(() => TestHTTP200(endpointBox.Text), 750); 
     if (this.currentTimeout != null) { 
      this.currentTimeout.Dispose(); 
      this.currentTimeout = timeout; 
     } 
    } 

這是最基本的原則,至少,每次重新開始一個750毫秒超時的用戶類型做你的事,並取消任何待定的計時器。

更新:完整的代碼示例:

public partial class Form1 : Form 
{ 
    private IDisposable currentTimeout; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void EndpointBox_KeyUp(object sender, KeyEventArgs e) 
    { 
     IDisposable timeout = TimerHelper.SetTimeout(() => TestHTTP200(EndpointBox.Text), 750); 
     if (this.currentTimeout != null) 
     { 
      this.currentTimeout.Dispose(); 
      this.currentTimeout = timeout; 
     } 
    } 

    private void TestHTTP200(string text) 
    { 
     //... 
    } 
} 

public class TimerHelper 
{ 
    public static IDisposable SetTimeout(Action method, int delayInMilliseconds) 
    { 
     System.Timers.Timer timer = new System.Timers.Timer(delayInMilliseconds); 
     timer.Elapsed += (source, e) => 
     { 
      method(); 
     }; 

     timer.AutoReset = false; 
     timer.Enabled = true; 
     timer.Start(); 

     // Returns a stop handle which can be used for stopping 
     // the timer, if required 
     return timer as IDisposable; 
    } 
} 
+0

這看起來很不錯。 'this.currentTimeout'從哪裏來? – SimonJGreen 2013-05-12 12:38:55

+0

對不起,我沒有提到它,將一個名爲currentTimeout的字段添加到您的IDisposable類型的類中,並且您很好。 – Bas 2013-05-12 12:49:37

+0

對不起,我不關注,你介意更新你的例子嗎? – SimonJGreen 2013-05-12 12:53:21