生成';忙';或者';工作';对话框,同时循环运行

本文关键字:运行 循环 工作 或者 生成 对话框 | 更新日期: 2023-09-27 18:30:04

我正在使用MSVS2010,在创建状态栏或状态条时遇到问题。我有一个循环,它做一些事情并运行一些函数。这是一个"while"循环。我所需要做的就是在这个循环中显示某种形式的"忙…"或"工作…"。是否有人对如何实现这一目标有具体的分步说明?我在网上看过教程和例子,但我不清楚如何做到这一点。这方面的任何帮助都将是伟大的。提前谢谢。

生成';忙';或者';工作';对话框,同时循环运行

您可以为其使用标签。只需添加一个标签并命名,例如stateLabel

然后在你的代码中这样做:

stateLabel.text = "Working..."; //Change the text of the Label
while(statement = true) //Your Loop
{
    //Do your work
}
stateLabel.text = "Finished work";

简单的做法是使用带有"marquee"样式属性的进度条,这可以让用户等待。因此,在循环之前设置"marquee"属性,然后在循环结束后可以返回默认值和/或隐藏此进度条。我是否足够清楚,或者你想要一些代码示例?

您可以做一些简单的事情,比如更改光标:

//change cursor to wait cursors
this.ForceCursor = Cursors.Wait;
//do loop
while(true)
{//do work}
//change the cursor back to regular arrow when work is completed
this.ForceCursor = Cursors.Arrow;

或者,只要在while循环中不使用UI控件,就可以在另一个线程上执行循环。只需在窗口中的某个地方放一个进度条(称为myProgBar),如果你不知道循环需要多长时间,就可以使其不确定。以下是我的做法:

//create instance of loading window
YourLoadWindow loadWin = new YourLoadWindow();
//Create a thread that will do our loop work.
Thread t = new Thread(new ThreadStart(() => 
{
//do our looping;
while (true) {//do work}
//when loop is done, we want to hide the loading window
//but we created it on a different thread, so we must use its dispatcher to do
//the work from this thread
loadWin.Dispatcher.Invoke(new Action(() => { loadWin.Close(); }));
}));
//show load window
loadWin.Show();
//start doing our work
t.Start();