简体   繁体   中英

Background worker thread exception

I'm using background worker in my application. There is no problem with background worker till i generate a report. After generating the devexpress report if i want to save the document the following error occurs.

"Current thread must be set to single thread apartment (STA) mode before OLE is required.Please ensure you have [STAThreadAttribute] in your main function."

I tried clearing the ddl's in /obj/ but it's not working.

My main code is :

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new FrmUxLogin());            
    }

my backgrounfworker code is(It is not a WinForm, it is a class) :

 public bool GoalReport(string val, string goalid)
    {            
        try
        {
            worker.WorkerReportsProgress = true;
            worker.DoWork += (obj, args) =>
            {
                //Performing calculations and generating report
            };
            worker.ProgressChanged += (obj, args) =>
                {
                    MDIMain objMain = Application.OpenForms["MDIMain"] as MDIMain;                       
                };
            worker.RunWorkerAsync();
        }
        catch(Exception)
        {
        }
     }
**My code for report generation is**
private void CreateSummaryGoalReportTables(DataTable dtcurrent, DataTable dtportfolio, DataTable achieveTargetPropbabilty)
    {// after clicking export on devexpress the exception occured here
    }<==ThreadStateException

The report is also getting generated but im not able to export the report. The devexpress has given the in built functions to save the report. I have not coded for report exporting. Please help!!! Thanks in advance

There is no way to setup background worker in to STA mode. You should call save operation from ui thread. Try just add onComplete handler and call save in it.

This article shows how to correctly write complete handler: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.runworkercompleted.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM