У меня есть обработчик события (uia) с измененным фокусом на фоновом потоке MTA для обновления списка элементов для текущего активного окна, которое запускается несколько раз в секунду при смене активного окна.Я пытаюсь заставить его идти один раз, а затем подождать одну секунду, прежде чем обрабатывать любые другие события.Проблема в том, что таймер запускается, но никогда не срабатывает?Я думаю, есть лучший способ сделать это?Пример кода был бы отличным.
public void HandleFocusChangedEvent(IUIAutomationElement sender)
{
// A focus changed event has been sent by the the active window or some descendant of it.
// Check that this event hasn't arrived around the time we're removing the event handler on shutdown.
if (!_fAddedEventHandler)
{
return;
}
// All the event handler needs to do is notify the main UI thread that the
// list of elements should be refreshed to make sure it's showing the most current list.
// We only want to do this once every second So use a timer/counter
if (focusChangedCounter == 0)
{
controllerDispatcher.BeginInvoke(_focusChangedEventHandlerDelegate);
focusChangedCounter = 1;
if (focusChangedBufferTimer == null)
{
focusChangedBufferTimer = new System.Windows.Forms.Timer();
focusChangedBufferTimer.Tick += new EventHandler(focusChangedBufferTimer_Tick);
focusChangedBufferTimer.Interval = 1000;
focusChangedBufferTimer.Start();
}
}
}
private void focusChangedBufferTimer_Tick ( object sender, EventArgs e)
{
focusChangedCounter = 0;
focusChangedBufferTimer.Stop();
focusChangedBufferTimer = null;
}