C# 在运行程序中等待一秒钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10458118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wait one second in running program
提问by Sel?uklu Ebrar
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?
? 想在使用此代码打印我的网格单元之前等待一秒钟,但它不起作用。我能做什么?
采纳答案by Matt Dawdy
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);
回答by Coder SAJDFJF
use dataGridView1.Refresh();:)
使用dataGridView1.Refresh();:)
回答by Mark Rowe
Personally I think Thread.Sleepis a poor implementation. It locks the UI etc. I personally like timer implementations since it waits then fires.
我个人认为Thread.Sleep是一个糟糕的实现。它锁定 UI 等。我个人喜欢计时器实现,因为它等待然后触发。
Usage: DelayFactory.DelayAction(500, new Action(() => { this.RunAction(); }));
用法: 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();
}
回答by Bartek Winslawski
Maybe try this code:
也许试试这个代码:
void wait (double x) {
DateTime t = DateTime.Now;
DateTime tf = DateTime.Now.AddSeconds(x);
while (t < tf) {
t = DateTime.Now;
}
}
回答by ChrisW
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;
....
回答by Said
Wait function using timers, no UI locks.
使用定时器的等待功能,没有 UI 锁定。
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
回答by Sava? SERTER
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
回答by Colin D
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.
我觉得这里的所有错误都是顺序,Sel?uklu 希望应用程序在填充网格之前等待一秒钟,所以 Sleep 命令应该在填充命令之前出现。
System.Threading.Thread.Sleep(1000);
dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red;
回答by Rich Hildebrand
.Net Core seems to be missing the DispatcherTimer.
.Net Core 似乎缺少DispatcherTimer.
If we are OK with using an async method, Task.Delaywill meet our needs. This can also be useful if you want to wait inside of a for loop for rate-limiting reasons.
如果我们可以使用异步方法,Task.Delay将满足我们的需求。如果您出于速率限制的原因想在 for 循环内等待,这也很有用。
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);
}

