尝试在Backgroundworker方法中显示报告时出现错误 [英] getting error when try to show report in Backgroundworker method

查看:104
本文介绍了尝试在Backgroundworker方法中显示报告时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Windows应用程序,我需要将数据保存到数据库中,并且在保存数据加载水晶报表以显示报表后,所有这些都将在单击保存按钮时发生.

单击此数据后,我有一个名为btn_Submit的按钮被保存并显示报告,同时保存需要花费时间,因此我想显示平均时间的进度条,以便用户知道数据正在处理中.
我能够使用将数据保存到db中,但是在显示名为 SalesReport 的报表时,出现错误消息
跨线程操作无效:从创建该线程的线程之外的线程访问"frmParent"控件.
在以下代码的 sreport .MdiParent = arg.parentf; 行中.

frmparent 是MDI表单.有什么解决办法吗?


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.

I have button named btn_Submit on 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
i was able to save data into db using but when displaying Report named SalesReport i got the error saying
Cross-thread operation not valid: Control ''frmParent'' accessed from a thread other than the thread it was created on.
on the line sreport .MdiParent = arg.parentf; in following code.

frmparent is MDI form. is there any solution?


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

推荐答案

您无法与后台线程中的UI项进行交互.对任何UI项的所有访问都必须在UI线程(您的应用开始于该线程)上完成.

行(包括SalesReport sreport ...)之后的所有代码都必须在UI线程上运行,而不是在BackgroundWorker中运行.

从此代码块的内容来看,没有理由让其余代码在BackgroundWorker上运行,因此您实际上不需要此代码的后台线程.
You cannot interact with UI items from a background thread. ALL access to ANY UI items must be done on the UI thread (the thread your app started on).

All of the code after and including the SalesReport sreport..." line MUST be run on the UI thread, not in the BackgroundWorker.

Judging by the content of this code block, there''s no reason for the remaining code to be run on a BackgroundWorker, so you really don''t need a background thread for this code.


您不能从未创建Windows UI组件的线程访问Windows UI组件(错误状态).

解决方案是使用委托返回UI线程或使用Progress update或complete事件.我为您推荐了最后一种,因为看来您已经完成了工作线程.
对于需要反馈的长期运行的过程(进度条),更新进度的作用更大.在某些情况下,使用委托是必要的,但这不是其中之一.

这是使用进度更改"事件和/或完成"事件的方式(应该使用完成"事件并将"SalesReport"代码放在此处)

You can not access Windows UI components from a thread that did not create it (as the error states).

The solution is to use a delegate to get back to the UI thread or use the Progress update or complete event. I recommend the last one for you case as it seems you are done with the worker thread.
Updating progress is more for long running process that you want feedback (i.e. progress bar). Using a delegate is necessary in some cases but this is not one of them.

Here is how you use the Progress change event and/or the completion event (you should use the completion event and put the "SalesReport" code there)

BackgroundWorker worker = new BackgroundWorker();

worker.ProgressChanged += worker_ProgressChanged;
worker.WorkerReportsProgress = true; //You need to set this if you require updates or you will get an exception
worker.RunWorkerCompleted += worker_RunWorkerCompleted;

...

void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//This is called after the DoWork method completes
   throw new NotImplementedException();
}

void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    //This is called anytime the worker thread calls "ReportProgress". Note you must have set WokerReportsProgress to true
    throw new NotImplementedException();
}


这篇关于尝试在Backgroundworker方法中显示报告时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆