1

我有 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.

这里应该做什么?

4

3 回答 3

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();
}
于 2012-11-27T08:17:30.657 回答
1

Try to reset the ProgressBar's Value property to 0, like in the code below:

Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);

Implemented event handler:

void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    progressBar1.Value = 0;
}
于 2012-11-27T08:14:05.063 回答
0

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

于 2012-11-27T07:36:00.573 回答