Wait one second while the program is running.

dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red; System.Threading.Thread.Sleep(1000); 

I want to wait one second before printing grid cells with this code, but it does not work. What can I do?

+74
c # wait
May 05 '12 at 1:47 a.m.
source share
8 answers

This is a pause, but you donโ€™t see your red color appearing in the cell? Try the following:

 dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red; dataGridView1.Refresh(); System.Threading.Thread.Sleep(1000); 
+128
May 05 '12 at 1:52
source share

Personally, I think Thread.Sleep is a bad implementation. It blocks the user interface, etc. I personally like the timer implementation, as it waits, then it fires.

Usage: DelayFactory.DelayAction(500, new Action(() => { this.RunAction(); }));

 //Note Forms.Timer and Timer() have similar implementations. public static void DelayAction(int millisecond, Action action) { var timer = new DispatcherTimer(); timer.Tick += delegate { action.Invoke(); timer.Stop(); }; timer.Interval = TimeSpan.FromMilliseconds(millisecond); timer.Start(); } 
+26
Dec 27 '13 at 10:30
source share

Waiting function using timers, without user interface locks.

 public void wait(int milliseconds) { System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer(); if (milliseconds == 0 || milliseconds < 0) return; //Console.WriteLine("start wait timer"); timer1.Interval = milliseconds; timer1.Enabled = true; timer1.Start(); timer1.Tick += (s, e) => { timer1.Enabled = false; timer1.Stop(); //Console.WriteLine("stop wait timer"); }; while (timer1.Enabled) { Application.DoEvents(); } } 

Usage: just putting this in your code to wait:

 wait(1000); //wait one second 
+8
Oct 20 '18 at 13:39
source share

Waiting for employment will not be a serious flaw if it is short. In my case, there was a need to give visual feedback to the user by flashing the control (this is a chart control that can be copied to the clipboard, which changes the background by a few milliseconds). It works great:

 using System.Threading; ... Clipboard.SetImage(bm); // some code distribution_chart.BackColor = Color.Gray; Application.DoEvents(); // ensure repaint, may be not needed Thread.Sleep(50); distribution_chart.BackColor = Color.OldLace; .... 
+2
Jun 29 '16 at 13:46 on
source share

use dataGridView1.Refresh(); :)

+1
Apr 14 '13 at 22:50
source share

Try this feature

 public void Wait(int time) { Thread thread = new Thread(delegate() { System.Threading.Thread.Sleep(time); }); thread.Start(); while (thread.IsAlive) Application.DoEvents(); } 

Call function

 Wait(1000); // Wait for 1000ms = 1s 
0
Mar 03 '19 at 23:30
source share

I feel that everything that was not right here is an order. Selchuklu wanted the application to wait a second before filling out the grid, so the Dream command should have come before the Fill command.

  System.Threading.Thread.Sleep(1000); dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red; 
0
Jul 15 '19 at 21:09
source share

Maybe try this code:

 void wait (double x) { DateTime t = DateTime.Now; DateTime tf = DateTime.Now.AddSeconds(x); while (t < tf) { t = DateTime.Now; } } 
-four
Nov 13 '14 at 20:51
source share



All Articles