1private bool WaitForSuccess(Func<bool> action, int millsecondTimeout = 2000)
2{
3 var start = DateTime.Now;
4 while ((DateTime.Now - start).TotalMilliseconds <= millsecondTimeout)
5 {
6 if (action()) return true;
7 Task.Delay(1); // or Thread.Sleep(1);
8 }
9 return false;
10}