What is the best way to wait a certain time (say 10 seconds) in C #?

I want to wait 15 seconds, then the control should resume from the next statement.

I have nothing to do while you wait (just waiting).

I know there is Thread.Sleep(15000) . What I do not know is the best way to wait? What are the limitations of this?

The code will look like this:

 Method() { statement 1; statement 2; //WaitFor 15 secs here; statement 3; } 
+6
c #
source share
8 answers

Try the following:

 void Method() { console.log('statement 1'); console.log('statement 2'); var timer = new System.Threading.Timer( o => // timer callback { console.log('statement 2'); }, 15000, // Delay 0 // Repeat-interval; 0 for no repeat ); } 

Syntax C # 3.0 uses the lambda expression to efficiently create a closure around statement # 3. In doing so, you can use any local method variables. However, it should be noted that using this method or any other timer-based method ... the function will return immediately after creating the timer. The function will not be blocked until the timer runs. For this, the only thing I can think of is to actually use the threads and make the Method () block on the signal (i.e. WaitHandle, ResetEvent, etc.) until the time-call of another thread is completed .

+2
source share

The disadvantage of Thread.Sleep is that it is called in your GUI thread (a thread that processes GUI events, for example, a button handler method or a method called using a button click handler, etc.), then you will freeze the application and doesn't respond to those 15 seconds.

It would be great if you explicitly created a separate thread and named Thread.Sleep in it, assuming that you don't mind the thread doing nothing for 15 seconds.

An alternative would be to create a timer and start it after stmt 2 and put stmt 3 in the Tick event handler for the timer, and also stop the timer in this handler.

+11
source share

This may not be the direct answer to your question. I would say check if your process flow is better than code validation; -)

Do you wait 15 seconds to make sure stmt2; completed? If so, then adding a handler as soon as stmnt 2 is executed would be a better solution (?)

You can also use a timer to wait. Thread.sleep - bad design. We have a similar question that talks about a comparison using Thread.sleep and Timer .

+6
source share

Thread.sleep seems like a smart task if there is nothing left to do while waiting. It causes the thread to sleep during this time, so it does not use CPU resources.

+2
source share

You can always use a timer and then execute the code after a set duration. However, if you really do not need to do anything and just need to wait at a certain point in the code, I think Thread.Sleep (150000); enough. [Edit: spelling]

+1
source share

If you always want to wait a while, it is useful to use Sleep . Obviously, you should not do this in a thread where timely responses are expected.

Keep in mind that your flow will sleep all the time. If for some reason you want the thread to resume earlier, you'd better use signaling or callbacks. Using any of them instead of Sleep , you minimize unnecessary waiting time.

0
source share
 void Method() { Statement1(); Statement2(); // Start the timer for a single 15 second shot. // Keep a reference to it (Mytimer) so that the timer doesn't get collected as garbage Mytimer = new System.Threading.Timer((a) => { // Invoke the 3rd statement on the GUI thread BeginInvoke(new Action(()=>{ Statement3(); })); }, null, 15000, // 15 seconds System.Threading.Timeout.Infinite); // No repeat } 
0
source share

I'm not 100% sure, but if you really need your method to return after waiting 15 seconds, try the following:

 Method() { stmt1(); stmt2(); int time = DateTime.Now.Millisecond; while (15*1000 > DateTime.Now.Millisecond - time) { Thread.Sleep(10) Application.DoEvents(); } stmt3(); }
Method() { stmt1(); stmt2(); int time = DateTime.Now.Millisecond; while (15*1000 > DateTime.Now.Millisecond - time) { Thread.Sleep(10) Application.DoEvents(); } stmt3(); } 
-one
source share

All Articles