问题
dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red;
System.Threading.Thread.Sleep(1000);
İ want to wait one second before printing my grid cells with this code, but it isn't working. What can i do?
回答1:
Is it pausing, but you don't see your red color appear in the cell? Try this:
dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red;
dataGridView1.Refresh();
System.Threading.Thread.Sleep(1000);
回答2:
Personally I think Thread.Sleep is a poor implementation. It locks the UI etc. I personally like timer implementations since it waits then 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();
}
回答3:
Wait function using timers, no UI 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 placing this inside your code that needs to wait:
wait(1000); //wait one second
回答4:
Busy waiting won't be a severe drawback if it is short. In my case there was the need to give visual feedback to the user by flashing a control (it is a chart control that can be copied to clipboard, which changes its background for some milliseconds). It works fine this way:
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;
....
回答5:
use dataGridView1.Refresh(); :)
回答6:
Try this function
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
回答7:
I feel like all that was wrong here was the order, Selçuklu wanted the app to wait for a second before filling in the grid, so the Sleep command should have come before the fill command.
System.Threading.Thread.Sleep(1000);
dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red;
回答8:
.Net Core seems to be missing the DispatcherTimer.
If we are OK with using an async method, Task.Delay will meet our needs. This can also be useful if you want to wait inside of a for loop for rate-limiting reasons.
public async Task DoTasks(List<Items> items)
{
foreach (var item in items)
{
await Task.Delay(2 * 1000);
DoWork(item);
}
}
You can await the completion of this method as follows:
public async void TaskCaller(List<Item> items)
{
await DoTasks(items);
}
回答9:
Maybe try this code:
void wait (double x) {
DateTime t = DateTime.Now;
DateTime tf = DateTime.Now.AddSeconds(x);
while (t < tf) {
t = DateTime.Now;
}
}
来源:https://stackoverflow.com/questions/10458118/wait-one-second-in-running-program