lunes, 14 de diciembre de 2015

Non blocking Sleep Windows Forms

Non blocking Sleep
Windows Forms.
Código para hacer sleep (Thread.Sleeip) sin bloquear la interfaz gráfica.
Sin usar await ni Task.Delay
Usar expresiones lambda en lugar de delegates
C# wait for a while without blocking.
Wait some seconds without blocking UI execution
Without using await and task delay
use lambda expressions instead of delegates



private static void NoBlockingSleep(int interval) { bool bTimerStarted = true; System.Windows.Forms.Timer t = new System.Windows.Forms.Timer(); t.Interval = interval; t.Tick += (x, y) => { t.Stop(); bTimerStarted = false; }; t.Start(); while (bTimerStarted) { Application.DoEvents(); } }