在 c# winforms 中显示加载或进度条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13579196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
show loading or progress bar in c# winforms
提问by user1850707
I have windows application in which i need to save data into database and after saving data load crystal report to show report,all this happens on click of save button.
我有 Windows 应用程序,我需要将数据保存到数据库中,并在保存数据后加载水晶报告以显示报告,所有这一切都发生在单击保存按钮时。
I have button named btn_Submiton click of this data is saved and display report, while saving it takes time so i want to show progress bar for mean time so that user get known that data is in process.how i can do with this windows application.
我有btn_Submit点击此数据时命名的按钮被保存并显示报告,而保存它需要时间,所以我想显示平均时间的进度条,以便用户知道数据正在处理中。我可以用这个 Windows 应用程序做什么。
I gone through this link http://www.codeproject.com/Tips/83317/BackgroundWorker-and-ProgressBar-demobut don't get it exactly I want.
我浏览了这个链接http://www.codeproject.com/Tips/83317/BackgroundWorker-and-ProgressBar-demo但没有得到我想要的。
I am aware of background worker but never used it.
我知道后台工作人员,但从未使用过它。
I have used background worker and progress bar as given in above link but progress bar does not stop at all once it started.
我已经使用了上面链接中给出的后台工作程序和进度条,但进度条一旦启动就不会停止。
Can any one help me?can u give any example or link that demonstrate scenario?.
任何人都可以帮助我吗?你能举出任何例子或链接来演示场景吗?。
This code i added on Dowork();
我在 Dowork() 上添加了这段代码;
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
PrintData arg = (PrintData)e.Argument;
SalesMaster sm = arg.SalesData;
BrokerMaster bm = arg.Broker;
CustomerMaster ctm = arg.Customer;
CompanyMaster cm = arg.Company;
ArrayList hb = arg.Arrardata;
int totunit = arg.totunit;
decimal globalamt = arg.golbamt;
SalesReport sreport = new SalesReport(sm, ctm, cm, bm, hb, totunit, glb_totalamt);
sreport .MdiParent = arg.parentf;
sreport .WindowState = FormWindowState.Maximized;
sreport .Show();
}
i get error on this line sreport .MdiParent = arg.parentf;
我在这一行sreport .MdiParent = arg.parentf;上得到错误
This error:
这个错误:
Cross-thread operation not valid: Control 'frmParent' accessed from a thread other than the thread it was created on.
what should be done here?
这里应该做什么?
回答by nilobarp
To stop or hide the progressbar use background worker's completed event.
要停止或隐藏进度条,请使用后台工作人员的完成事件。
回答by Dimitri Toonen
Try to reset the ProgressBar's Value property to 0, like in the code below:
尝试将 ProgressBar 的 Value 属性重置为 0,如下面的代码所示:
Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);
Implemented event handler:
已实现的事件处理程序:
void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar1.Value = 0;
}
回答by Sergey Berezovskiy
Suscribe to DoWorkand RunWorkerCompletedevents and
订阅DoWork和RunWorkerCompleted事件和
void btn_Submit_Click(object sender, EventArgs e)
{
btn_Submit.Enabled = false; // disable button while saving report
lbl_Status.Text = "Please wait..";
backgroundWorker.RunWorkerAsync();
}
void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
// save report here
}
void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
btn_Submit.Enabled = true; // enable button
lbl_Status.Text = "Report saved";
}
Instead of using label, you can show PictureBoxwith spinner wait image. I don't like to see progress bar, which does not show percentage of task - I expect that when progress bar will be filled, task will be completed. If you really want to use progress bar, then, I'd go with timer component (set timer's interval to desired refresh rate):
您可以PictureBox使用微调器等待图像显示,而不是使用标签。我不喜欢看到进度条,它不显示任务的百分比 - 我希望当进度条被填满时,任务将完成。如果您真的想使用进度条,那么,我会使用计时器组件(将计时器的间隔设置为所需的刷新率):
void btn_Submit_Click(object sender, EventArgs e)
{
btn_Submit.Enabled = false; // disable button while saving report
timer.Start();
progressBar.Visible = true;
// backgroundWorker.RunWorkerAsync(new object[] { "Foo", 42 });
// backgroundWorker.RunWorkerAsync(new CustomType("Foo", 42));
backgroundWorker.RunWorkerAsync(new { Foo = "Foo", Bar = 42 }););
}
void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
// object[] args = (object[])e.Argument;
// CustomType arg = (CustomType)e.Argument;
dynamic arg = (dynamic)e.Argument;
string foo = arg.Foo;
int bar = arg.Bar;
// save report here
}
void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
btn_Submit.Enabled = true; // enable button
timer.Stop();
progressBar.Visible = false;
}
void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value == progressBar.Maximum)
{
progressBar.Value = progressBar.Minimum;
return;
}
progressBar.PerformStep();
}

