C# 如何定期更新Label控件的值?

C# 如何定期更新Label控件的值?,c#,winforms,delay,C#,Winforms,Delay,我试图让一个标签显示一些文本,然后在一段时间后刷新自己,并能够在以后重新显示其他内容。然而,目前我不知道如何使标签暂停(如果可能的话) 到目前为止,我的代码是: foreach (var x in mod) { labelWARNING.Visible = true; labelWarningMessage.Text = "This module has a prerequisite module: " + x; //need a pause here to

我试图让一个标签显示一些文本,然后在一段时间后刷新自己,并能够在以后重新显示其他内容。然而,目前我不知道如何使标签暂停(如果可能的话)

到目前为止,我的代码是:

foreach (var x in mod)
{
      labelWARNING.Visible = true;
      labelWarningMessage.Text = "This module has a prerequisite module: " + x;
      //need a pause here to give user sufficient time to read the above text
      //labelWarningMessage.Text = "";
}

我会使用另一个线程:

labelWARNING.Visible = true;
labelWarningMessage.Text = "This module has a prerequisite module: " + item;
new Thread(() => {
    Thread.Sleep(5000);
    Dispatcher.BeginInvoke((Action)(() => { labelWARNING.Visible = false; }));
}).Start();

(这是针对WPF的,对于WinForms基本上应该是相同的)

从您的问题来看,似乎需要更改状态标签之类的值,以便定期向用户显示信息。如果您使用的是winforms,则可以使用计时器和委托,如下所示:

//First create a delegate to update the label value
public delegate void labelChanger(string s);

//create timer object
Timer t = new Timer();

//create a generic List to store messages. You could also use a Queue instead.
List<string> mod = new List<string>();

//index for list
int cIndex = 0;

//add this in your Form Load event or after InitializeComponent()
t.Tick += (timer_tick);
t.Interval = 5000;//how long you want it to stay.
t.Start();

//the timer_tick method
private void timer_tick(object s, EventArgs e)
{
     labelWarningMessage.Invoke(new labelChanger(labelWork), mod[cIndex]);
     cIndex++;
}

//the method to do the actual message display
private void labelWork(string s)
{
     labelWARNING.Visible = true;
     labelWarningMessage.Text = "This module has a prerequisite module: " + s;
}

从您的问题来看,似乎需要更改状态标签之类的值,以便定期向用户显示信息。如果您使用的是winforms,则可以使用计时器和委托,如下所示:

//First create a delegate to update the label value
public delegate void labelChanger(string s);

//create timer object
Timer t = new Timer();

//create a generic List to store messages. You could also use a Queue instead.
List<string> mod = new List<string>();

//index for list
int cIndex = 0;

//add this in your Form Load event or after InitializeComponent()
t.Tick += (timer_tick);
t.Interval = 5000;//how long you want it to stay.
t.Start();

//the timer_tick method
private void timer_tick(object s, EventArgs e)
{
     labelWarningMessage.Invoke(new labelChanger(labelWork), mod[cIndex]);
     cIndex++;
}

//the method to do the actual message display
private void labelWork(string s)
{
     labelWARNING.Visible = true;
     labelWarningMessage.Text = "This module has a prerequisite module: " + s;
}
//首先创建一个委托来更新标签值
公共委托无效标签更改器(字符串s);
//创建计时器对象
定时器t=新定时器();
//创建一个通用列表来存储消息。您也可以使用队列代替。
List mod=新列表();
//列表索引
int-cIndex=0;
//将其添加到表单加载事件中或InitializeComponent()之后
t、 滴答声+=(计时器滴答声);
t、 间隔=5000//你想让它停留多久。
t、 Start();
//计时器滴答法
私有无效计时器(对象s、事件参数e)
{
调用(新的labelChanger(labelWork),mod[cIndex]);
cIndex++;
}
//执行实际消息显示的方法
专用空白标签(字符串s)
{
labelWARNING.Visible=true;
labelWarningMessage.Text=“此模块有一个必备模块:”+s;
}
我希望这有帮助。祝你好运

编辑:我想我很久以前就发布了这段代码,但回来后发现我没有发布……但也许有人会发现它很有用

此外,在这种情况下,此方法将是多余的,因为单独创建计时器将在不使用委托的情况下工作,并且仅对委托使用和UI线程访问部分有用。我使用了计时器

private void timer1_Tick(object sender, EventArgs e)
{        
      labelWarningMessage.Text = "";
      labelWARNING.Visible = false;
}

已为异步/等待更新。这将

// Update all text with warning message
foreach (var x in mod)
{
      labelWARNING.Visible = true;
      labelWarningMessage.Text = "This module has a prerequisite module: " + x;
}

// Wait 1 second or 1000ms    
await Task.Delay(1000);

// Now dismiss the message
foreach (var x in mod)
      labelWarningMessage.Text = "";
周围的函数需要是
公共异步任务SomeMethod()

public async void按钮单击(对象发送者,路由目标)


若要使用
wait
关键字,暂停将无济于事,您会阻止UI线程,因此新文本甚至不会显示。即使在foreach循环中,它的条件是什么?这是winform、wpf还是asp.net?AZ1-不要在中编辑答案,发布一个自我回答。或者使用一个更简单的计时器,因为它将在UI线程上回调。@usr:确实:)您介意将代码作为回答吗?因此OP将有更多的变体可供学习。