How to wait for the timer to end and then return from the function?

I want to wait for the timer to end and return from the function. I tried this:

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

But that did not work, and the function returned before the timer really ended. Here's the full function:

 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; } 
+5
source share
5 answers

I think you really want to wait until the browser is ready. If so, you can use such a loop, and instead of Application.DoEvents() I recommend using 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 

Or better, you can handle the DocumentCompleted WebBrowser event.

+5
source

I will not go into how good or bad it is. But you can just do it with a flag.

 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; } 

And you should probably put Thread.Sleep in a while loop.

+2
source

Instead of Timer you can use Thread . Run the wait method on the thread and just wait until it finishes with thread.IsAlive .

Your code should look like this:

 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; 
+1
source

Instead of using Await / Async and changing the method signature, you can use CountdownEvent and threading.

 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 blocks the current thread until the counter reaches 0. Since the Timer class uses ThreadPool for its callback, this event still fires and works as usual.

+1
source

Just put some_value in the class level variable, then set a timer on the event. Any function that calls your function can (before the call) subscribe to the event and then process it.

+1
source

Source: https://habr.com/ru/post/1206673/


All Articles