在c#winforms中显示加载或进度条

时间:2012-11-27 07:18:27

标签: c# winforms progress-bar backgroundworker

我有windows应用程序,我需要将数据保存到数据库中,并在保存数据加载水晶报告显示报告后,所有这些都发生在点击保存按钮。

点击此数据时,我有一个名为 btn_Submit 的按钮被保存并显示报告,同时保存它需要时间,因此我想显示平均时间的进度条以便用户知道数据正在进行中。我可以使用这个Windows应用程序。

我浏览了这个链接http://www.codeproject.com/Tips/83317/BackgroundWorker-and-ProgressBar-demo,但是我没想得到它。

我知道后台工作者但从未使用它。

我使用了上面链接中给出的后台工作符和进度条,但进度条一旦开始就不会停止。

任何人都可以帮助我吗?你能举出任何展示场景的例子或链接吗?。

我在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();
}

我在此行收到错误 sreport .MdiParent = arg.parentf;

此错误:

Cross-thread operation not valid: Control 'frmParent' accessed from a thread other than the thread it was created on.

应该在这做什么?

3 个答案:

答案 0 :(得分:3)

接受DoWorkRunWorkerCompleted事件以及

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";
}

您可以使用微调器等待图像显示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();
}

答案 1 :(得分:1)

尝试将ProgressBar的Value属性重置为0,如下面的代码所示:

Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);

已实施事件处理程序:

void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    progressBar1.Value = 0;
}

答案 2 :(得分:0)

要停止或隐藏进度条,请使用后台工作人员已完成的事件。