dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red;
System.Threading.Thread.Sleep(1000);
我想在打印格子之前等待一秒钟,但这段代码没有生效。我该怎么做?
dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red;
System.Threading.Thread.Sleep(1000);
我想在打印格子之前等待一秒钟,但这段代码没有生效。我该怎么做?
它是否暂停了,但您没有看到单元格中出现红色?尝试这个:
dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red;
dataGridView1.Refresh();
System.Threading.Thread.Sleep(1000);
个人认为Thread.Sleep
是一种较差的实现方式,会锁定用户界面等。我个人喜欢计时器实现,因为它能够等待并触发事件。
用法: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();
}
使用定时器的等待函数,没有UI锁定。
public void wait(int milliseconds)
{
var 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();
}
}
用法:将这段代码放入需要等待的代码中即可:
wait(1000); //wait one second
看起来.Net Core缺少DispatcherTimer
。
如果我们可以使用异步方法,Task.Delay
将满足我们的需求。 如果您想因限速原因在for循环中等待,这也可能非常有用。
public async Task DoTasks(List<Items> items)
{
foreach (var item in items)
{
await Task.Delay(2 * 1000);
DoWork(item);
}
}
你可以按照以下方式等待该方法的完成:
public async void TaskCaller(List<Item> items)
{
await DoTasks(items);
}
最佳的等待方式是使用 Task.Delay 函数来避免阻塞主线程。
因此,您的代码将如下所示:
var t = Task.Run(async delegate
{
dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red;
dataGridView1.Refresh();
await Task.Delay(1000);
});
我觉得这里的问题只是顺序有误,Selçuklu想让应用在填充网格之前等待一秒钟,因此Sleep命令应该在填充命令之前。
System.Threading.Thread.Sleep(1000);
dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red;
如果使用异步
await Task.Delay(new TimeSpan(0, 0, 1));
否则
Thread.Sleep(new TimeSpan(0, 0, 1));
如果忙等待时间很短,那么它不会成为一个严重的缺点。在我的案例中,需要通过闪烁控件来向用户提供视觉反馈(这是一个图表控件,可以复制到剪贴板,在某些毫秒内更改其背景)。这种方法效果很好:
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;
....
dataGridView1.Refresh();
:)public void safeWait(int milliseconds)
{
long tickStop = Environment.TickCount + milliseconds;
while (Environment.TickCount < tickStop)
{
Application.DoEvents();
}
}