wait

How to asynchronously wait for x seconds and execute something then?

淺唱寂寞╮ 提交于 2019-11-27 20:37:58
I know there is Thread.Sleep and System.Windows.Forms.Timer and Monitor.Wait in C# and Windows Forms. I just can't seem to be able to figure out how to wait for X seconds and then do something else - without locking the thread. I have a form with a button. On button click a timer shall start and wait for 5 seconds. After these 5 seconds some other control on the form is colored green. When using Thread.Sleep , the whole application would become unresponsive for 5 seconds - so how do I just "do something after 5 seconds"? eFloh (transcribed from Ben as comment) just use System.Windows.Forms

Why doesn't thread wait for notify()?

不想你离开。 提交于 2019-11-27 18:48:57
问题 Why doesn't thread wait for notify() ? The thread starts and then goes to the waiting pool, but it proceeds to execute after that moment. public class JavaApplication2 { public static void main(String [] args) { ThreadB b = new ThreadB(); synchronized(b) { b.start(); try { System.out.println("1"); b.wait(); } catch (InterruptedException e) {} System.out.println("Total is: " + b.total); } } } class ThreadB extends Thread { int total; @Override public void run() { synchronized(this) { total +=

how to use wait in C

纵饮孤独 提交于 2019-11-27 18:22:54
问题 How do i use wait ? It just baffles me to no end. I fork a tree of procs with recursion and now the children have to pause(wait/sleep) while I run pstree so I can print the proc tree. Should i use int status; wait(&status); or rather wait(NULL) and where should i put this? in the parent if(pid > 0) or in the children if(pid==0) ? Maybe at the end of ifs, so I store all the pid s in array and then run a for over them and use wait? my code template: void ProcRec(int index) { pid_t pid; int

Bash: Sleep until a specific time/date

丶灬走出姿态 提交于 2019-11-27 17:19:38
I want my bash script to sleep until a specific time. So, I want a command like "sleep" which takes no interval but an end time and sleeps until then. The "at"-daemon is not a solution, as I need to block a running script until a certain date/time. Is there such a command? SpoonMeiser As mentioned by Outlaw Programmer, I think the solution is just to sleep for the correct number of seconds. To do this in bash, do the following: current_epoch=$(date +%s) target_epoch=$(date -d '01/01/2010 12:00' +%s) sleep_seconds=$(( $target_epoch - $current_epoch )) sleep $sleep_seconds To add precision down

Python wait x secs for a key and continue execution if not pressed

折月煮酒 提交于 2019-11-27 16:42:09
问题 I'm a n00b to python, and I'm looking a code snippet/sample which performs the following: Display a message like "Press any key to configure or wait X seconds to continue" Wait, for example, 5 seconds and continue execution, or enter a configure() subroutine if a key is pressed. Thank you for your help! Yvan Janssens 回答1: If you're on Unix/Linux then the select module will help you. import sys from select import select print "Press any key to configure or wait 5 seconds..." timeout = 5 rlist,

Java并发编程初级篇(三):线程状态以及状态转换过程

醉酒当歌 提交于 2019-11-27 16:04:47
线程状态: NEW:当一个线程被创建之初,那么此时线程就是新生状态,此状态下线程已经分配完毕内存空间。 RUNNABLE:当调用Thread.start()方法后,线程进入就绪状态,此时线程并不会马上开始执行。需要等待JVM选中并分配CPU时间才能开始执行。 RUNNING:线程被分配CPU时间后,进入执行状态,在此状态下会运行run()方法中定义的代码。 BLOCKED:当处于运行状态的线程,调用一个被阻塞的方法;试图获取一个正在被其他线程占用的锁,都会进入阻塞状态。 WAITING:等待状态。当调用了 Object.wait() ; Thread.join() ; LockSupport.park() ;方法后线程会进入等待状态。调用 Object.wait() 的线程会等待 Object.notify() 方法的调用而重新进入就绪状态。调用 Thread.join() 方法的线程会等待调用方法线程执行结束而进入结束状态。 TIMED_WAITING:固定时间等待状态。此状态下的线程都有一个固定的等待时间,通过调用 Object.wait(Long) , Thread.sleep(Long) , Thread.join(Long) ,都会让线程进入此状态。处于此状态的线程会等待指定的时间,然后恢复执行。 TERMINATED:线程执行结束。 状态转换图: 线程状态转换函数:

Wait for n seconds, then next line of code without freezing form

帅比萌擦擦* 提交于 2019-11-27 15:52:23
问题 Hi I am trying to find a method of waiting a number of milliseconds before moving to the next line of code, I have looked into Thread.Sleep but this will freeze the main form, I would like this to remain active. I tried timers and stopwatches and both freeze the main form when they should be posting to a console when they tick. I couldn't find a way of using task.delay or background worker in the wait I wanted either. Pseudo Code: Wait 2 - 6 seconds Log "waiting" Log "waiting" Log "waiting"

Wait for a while without blocking main thread

馋奶兔 提交于 2019-11-27 14:59:48
I wish my method to wait about 500 ms and then check if some flag has changed. How to complete this without blocking the rest of my application? Toneo Thread.Sleep(500) will force the current thread to wait 500ms. It works, but it's not what you want if your entire application is running on one thread. In that case, you'll want to use a Timer , like so: using System.Timers; void Main() { Timer t = new Timer(); t.Interval = 500; // In milliseconds t.AutoReset = false; // Stops it from repeating t.Elapsed += new ElapsedEventHandler(TimerElapsed); t.Start(); } void TimerElapsed(object sender,

Sending one AJAX request at a time from a loop

ぐ巨炮叔叔 提交于 2019-11-27 14:46:25
I know this question has been asked countless times, but I cant figure out for the life of me how to make this answer work in my case: wait for async javascript function to return I'm looping through some "tv channels" in the outerloop and then looping through dates in the week in the innerloop. In the inner loop I make a ajax request to a server to fetch the data and I then store/cache it for later use like so var dates = []; //<-- Contains a list of dates for the coming week var baseUrl = "http://www.someserver.com"; var storedChannels = [1,2,3,4,5,6,7,8,9,10,45,23,56,34,23,67,23,567,234,67

How to differentiate when wait(long timeout) exit for notify or timeout?

牧云@^-^@ 提交于 2019-11-27 14:24:40
问题 Having this wait declaration: public final native void wait(long timeout) throws InterruptedException; It could exit by InterruptedException, or by timeout, or because Notify/NotifyAll method was called in another thread, Exception is easy to catch but... There is any way to know if the exits cause was timeout or notify? EDIT: This is a tricky way that could work, (although I don't like it) long tBefore=System.currentTimeMillis(); wait(TIMEOUT); if ((System.currentTimeMillis() - tBefore) >