在上一次更改之后更改控件的文本之前等待 [英] Wait before changing a Control's text after previous change

查看:23
本文介绍了在上一次更改之后更改控件的文本之前等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望 Label 的文本在按下 Button 后随其颜色一起更改.
第一部分是为了将 TextForeColor 更改为 Color.Orange,然后在更改 之前等待 1 秒再次 Text 属性并将 ForeColor 设置为 Color.LightGreen.

I want the text of a Label to change along with its colour after a Button has been pressed.
The first part is meant to change the Text and also the the ForeColor to Color.Orange, then wait 1 second before changing the Text property again and setting ForeColor to Color.LightGreen.

我的问题是它没有按照这个顺序执行,它只是跳过第一部分并更改为第二部分(文本更改为已完成!"并且颜色更改为浅绿色).

My issue is that it doesn't do it in that order, it just skips the first part and changes to the 2nd part (the text changes to "finished!" and the colour to light green).

这是我的代码:

private void button1_Click(object sender, EventArgs e)
{
    label6.Text = "Working..";
    label6.ForeColor = Color.Orange;

    System.Threading.Thread.Sleep(1000);
    label6.Text = "Finsihed!";
    label6.ForeColor = Color.LightGreen;
}

推荐答案

你需要在睡觉前告诉你的标签更新

you need to tell your label to update before you do the sleep

private void button1_Click(object sender, EventArgs e)
{
    label6.Text = "Working..";
    label6.ForeColor = Color.Orange;
    label6.Update(); // tell the label to update now

    System.Threading.Thread.Sleep(1000);
    label6.Text = "Finished!";
    label6.ForeColor = Color.LightGreen;
    label6.Update(); // tell the label to update now
}

但是还有另一种方法可以做到这一点.当您使用 async 并使用 Task.Delay() 而不是 Thread.Sleep() 与您的点击方法时,无需调用Update() 语句.
这也让您的表单保持响应.

But there is another way to do this. When you use async and use Task.Delay() instead of Thread.Sleep() with your click method there is no need to call the Update() statement.
And this also keeps your form responsive.

以下是在您的情况下如何执行此操作的示例.

here is an example of how you can do this in your situation.

private async void button1_Click(object sender, EventArgs e)
{
    label6.Text = "Working..";
    label6.ForeColor = Color.Orange;

    await System.Threading.Tasks.Task.Delay(1000);
    label6.Text = "Finished!";
    label6.ForeColor = Color.LightGreen;
}

那么,Update()async 哪个更好?
嗯,这取决于

So, which is better, the Update() or the async ?
Well that depends

当您仅使用 UpdateThread.Sleep() 时,您的表单将根据需要进行更新.但是在您的 Thread.Sleep() 完成之前,它也不会响应任何内容.
这可能是一件坏事,但也可能是一件好事.
那要看你的情况了.也许您不希望用户在 Thread.Sleep() 完成之前能够做任何事情.但这也意味着用户不能将表单拖动到其他位置.这对用户来说可能很烦人.

When you just use the Update with Thread.Sleep() than your form will update, as you want. But it will also not respond to anything until your Thread.Sleep() has finished.
This can be a bad thing, but it can also be a good thing.
That depends on your situation. Maybe you don't want the users to be able to do anything at all until the Thread.Sleep() is finished. But it also means the user cannot drag the form to some other position. That might be annoying for the user.

当您使用 asyncTask.Delay() 时,您的表单也会根据需要更新.但它也会响应其他操作,例如拖动、调整大小、单击等...
如果你想要这个,那么这就是要走的路.
如果你不想要这个,那么要么再次使用 Update(),或者确保用户不能点击他不应该点击的任何东西.
后者是一个很好的解决方案,因为用户仍然可以将表单拖动到他想要的位置,为什么操作正在运行,但仍然不能做任何错误.
这意味着在您开始操作之前,禁用所有他应该无法点击的控件,然后开始操作,完成后再次启用所有这些控件.
同意,这是更多的工作,但对用户来说看起来更好.

When you use async and Task.Delay() then your form will also update, as you want. But it will also respond to other actions, like dragging, resizing, clicks, etc...
If you want this, then this is the way to go.
If you dont want this, then either use the Update() again, or make sure the user cannot click on anything he is not supposed to.
The latter is a nice solution, because the user will still be able to drag the form where he wants it whyle the operation is running, but still cannot do anything wrong.
This means that before you start the operation, disable all controls he should not be able to click on, then start the operation and when it finishes enable all these controls again.
Agreed, this is more work, but it looks much nicer for the user.

所以我认为使用 asyncTask.Delay() 是更好的解决方案.

So in my opinion using the async and Task.Delay() is the better solution.

这篇关于在上一次更改之后更改控件的文本之前等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆