你好,我有一个函数,需要时间来加载。这就是为什么我计划在我的winform上放一个进度条,这样用户就会知道我的程序还在运行。然而,我不知道我将如何解决它。这里有没有人能帮我指路。
以下是我计划做的事情:
private void btnProcess_Click(object sender, EventArgs e)
{
//function which takes time because it contacts to a server
}
我想要有一个进度条,它会在我的进程结束后递增和结束。我应该使用后台工作人员来做这个吗?
*我遵循了本教程http://www.codeproject.com/Tips/83317/BackgroundWorker-and-ProgressBar-demo,但它不会像加载屏幕那样等待特定函数或事件完成。
*我的进度条不会在我的buttonclick事件执行完它的所有函数后结束。
我创建了:
private void myBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 100; i++)
{
myBackgroundWorker.ReportProgress(i);
System.Threading.Thread.Sleep(100);
}
}
private void myBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
myProgressBar.Value = e.ProgressPercentage;
}
private void btnProcess_Click(object sender, EventArgs e)
{
myBackgroundWorker.RunWorkerAsync();
//function which takes time because it contacts to a server
}
我怎么知道我的buttonclick事件何时结束?这样我的进度条也会结束吗?
发布于 2013-05-02 10:27:23
是啊,你应该的。这很简单。
创建一个后台工作者,执行btnProcess_Click
所做的任何工作。
让它报告进度:
worker.WorkerReportsProgress = true;
现在,您可以通过订阅的事件触发此进度报告。
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
在这样做的时候,您可以创建一个进度条,该进度条可以根据计算触发的这个worker_ProgressChanged
事件进行自我更新。
你可以通过谷歌搜索找到大量的实现。祝你好运,希望这能帮上忙。
发布于 2013-05-02 10:36:34
这是两个非常好的例子。
http://www.dreamincode.net/forums/topic/112547-using-the-backgroundworker-in-c%23/
http://www.dreamincode.net/forums/topic/246911-c%23-multi-threading-in-a-gui-environment/
希望这能有所帮助
编辑:
public partial class Form1 : Form
{
//--------------------------------------------------------------------------
public Form1()
{
InitializeComponent();
//Initialize backgroundworker
Shown += new EventHandler(Form1_Shown);
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged +=
new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
//counter
YourObject.Counter = 100;
}
//--------------------------------------------------------------------------
void btnClick_Clicked(object sender, EventArgs e)
{
//Trigger the background process
backgroundWorker1.RunWorkerAsync();
}
//--------------------------------------------------------------------------
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//Your freakishly long process,
//which needs to be run in the background
//to make it simple, I'll just do a for loop
for (int i = 0; i < 100; i++)
{
//The counter will keep track of your process
//In your case, it might not be a for loop
//So you will have to decide how to keep track of this counter
//My suggestion is to decrease/increase this counter
//right after importants action of your process
backgroundWorker1.ReportProgress(YourObject.Counter--);
}
}
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//The counter is updated and displayed with a progress bar
YourObject.Counter = e.ProgressPercentage;
}
}
https://stackoverflow.com/questions/16329378
复制相似问题