如何等待计时器完成,然后从函数返回

本文关键字:然后 返回 函数 计时器 何等待 等待 | 更新日期: 2023-09-27 18:30:01

我想等待计时器结束,然后从函数返回。我试过这个:

while(timer1.Enabled)
{
    Application.DoEvents();
}
return value;

但它没有工作,在计时器真正结束之前,函数就返回了。以下是完整的功能:

System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
int[] foo()
{
    do_something1();
    int[] some_value;
    timer1.Interval = 1000;
    timer1.Tick += new EventHandler(delegate(object o, EventArgs ea)
    {
        if (browser.ReadyState == WebBrowserReadyState.Complete)
        {
            timer1.Stop();
            baa(some_value);
        }
    });
    timer1.Start();
    while (timer1.Enabled)
    {
        Application.DoEvents();
    }
    return some_value;
}

如何等待计时器完成,然后从函数返回

我认为您真正想要的是等到浏览器准备好。如果是这样的话,你可以使用这样的循环,而不是Application.DoEvents(),我建议使用async/await功能:

while(browser.ReadyState != WebBrowserReadyState.Complete)
{ 
   // you need to make your method async in order to use await
   await Task.Delay(1000);
}
// do your job

或者您可以更好地处理WebBrowser的DocumentCompleted事件。

您可以使用Thread而不是Timer。在线程中启动等待方法,然后等待它以thread.IsAlive结束。

您的代码必须如下所示:

int[] some_value;
int intervalle = 1000;
Thread thread = new Thread(new ThreadStart(delegate()
{
    while (true)
    {
        Thread.Sleep(intervalle);
        if (browser.ReadyState == WebBrowserReadyState.Complete)
        {
            baa(some_value);
            return;
        }
    }                
}));
thread.Start();
while (thread.IsAlive)
{
    Application.DoEvents();
}
return some_value;

您可以使用CountdownEvent和线程,而不是使用Await/Async和更改方法签名。

using System.Theading;
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
int[] foo()
{
  int numberOfIterations = interval time * number of intervals; // initialize this to the proper number of times to decrement the counter. If we are decrementing every 1000 ms
  CountdownEvent countDown = new CountdownEvemt(numberOfIterations);
  do_something1();
  int[] some_value;
  timer1.Interval = 1000;
  timer1.Tick += new EventHandler(delegate(object o, EventArgs ea)
  {
      if (browser.ReadyState == WebBrowserReadyState.Complete)
      {
          timer1.Stop();
          baa(some_value);
          // since this seems where you want to shut down the method we need to trigger the countdown
          int countLeft = countDown.CurrentCount;
          countDown.Signal(countLeft); 
      } else {
          if(countDown.CurrentCount - 1 == 0) {
              countDown.Reset(); // we don't want to finish yet since we haven't reached the proper end condition so reset the counter
          }
          countDown.Signal(); // will decrement the counter
      }
  });
  timer1.Start();
  countDown.Wait(); // will wait 'til the counter is at 0 before continuing
   return some_value;

}

CountdownEvent将阻塞当前线程,直到计数器达到0。由于Timer类使用ThreadPool进行回调,因此该事件仍将激发并正常工作。

我不会讨论这种方法的好坏。但你可以简单地用一个标志来做这件事。

System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
int[] foo()
{
    do_something1();
    int[] some_value;
    bool doneWaiting = false;
    timer1.Interval = 1000;
    timer1.Tick += new EventHandler(delegate(object o, EventArgs ea)
    {
        if (browser.ReadyState == WebBrowserReadyState.Complete)
        {
            timer1.Stop();
            baa(some_value);
            doneWaiting = true;
        }
    });
    timer1.Start();
    while (!doneWaiting)
    {
        Application.DoEvents();
    }
    return some_value;
}

你可能应该放一个线程。也可以在while循环中睡觉。

只需将some_value放入类级变量中,然后让计时器引发一个事件。任何调用函数的函数都可以(在调用之前)订阅事件,然后进行处理。