C# 在后台工作程序中如何调用另一个WPF窗口

C# 在后台工作程序中如何调用另一个WPF窗口,c#,wpf,dialog,backgroundworker,C#,Wpf,Dialog,Backgroundworker,我有一个WPF应用程序,它允许用户选择一对多的电子表格,并执行一个清理数据和写出管道分隔文件的过程。我试图使用后台工作程序来显示进度条。我遇到的问题是,我有backgroundworker\u DoWork例程来执行读取excel文件、清理数据和写出数据的操作。对于每个电子表格,用户可以指定数据的范围,或者让Excel决定范围。我为这个输入调用一个对话框。我得到了一个错误“调用线程必须是STA,因为许多UI组件都需要它。”我一直试图找到一个解决方案,但我尝试的一切都不起作用 这是我的密码。我已经

我有一个WPF应用程序,它允许用户选择一对多的电子表格,并执行一个清理数据和写出管道分隔文件的过程。我试图使用后台工作程序来显示进度条。我遇到的问题是,我有backgroundworker\u DoWork例程来执行读取excel文件、清理数据和写出数据的操作。对于每个电子表格,用户可以指定数据的范围,或者让Excel决定范围。我为这个输入调用一个对话框。我得到了一个错误“调用线程必须是STA,因为许多UI组件都需要它。”我一直试图找到一个解决方案,但我尝试的一切都不起作用

这是我的密码。我已经将注释放在了有问题的代码所在的位置,以及到目前为止我所尝试的内容

private void ProcessFiles()
    {
        int intNumFiles = 0;
        string strFileType = "";
        int intPos = 0;
        string strMsg = "";


        intNumFiles = strExFileNames.Length;

        foreach (string strInputFile in strExFileNames)
        {
            intPos = strInputFile.LastIndexOf(".");
            strFileType = strInputFile.Substring(intPos + 1);

            if(!blnEXTextFileFound)
            {
                strExcelInputFile = strInputFile;

                if (blnExParamCancel || blnMWCancel)
                {
                    strMsg = "Processing has been cancelled.";
                    System.Windows.MessageBox.Show(strMsg);
                    break;
                }

                prgExcelProgress.Visibility = Visibility.Visible;
                txtExcelPercent.Visibility = Visibility.Visible;
                EnterExcelStateRunning();

                try
                {
                    bgwExcelRunner.RunWorkerAsync(strInputFile);
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show("Trying to run the Excel/Text file clean process resulted in this error: " + ex.Message, " Error");
                    prgExcelProgress.Foreground = new SolidColorBrush(Colors.DarkRed);
                    prgExcelProgress.Value = 100;
                }

                //ReadWriteExcelData(strInputFile);
            }
            else
            {
                if (blnEXTextFileFound)
                {
                    ProcessDelimitedFile(strInputFile);
                }
                else
                {
                    strMsg = "File type " + strFileType + " is not supported.";
                    strMsg += " Please contact support.";
                    System.Windows.MessageBox.Show(strMsg);
                }
            }
        }

        prgExcelProgress.Value = 100;


        //prgIndicator.Width = 400;
        //lblPrctPrgrs.Content = "100%";
        //grdProgressIndicator.InvalidateVisual();
        //System.Windows.Forms.Application.DoEvents();
        //Thread.Sleep(2000);
        //txtIndicator.Text = "All files have been processed and created in " + strExOutputPath + ".";

        blnDoForAll = false;
    }

private void bgwExcelRunner_DoWork(object sender, DoWorkEventArgs e)
    {
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        Excel.Range range, colrange, rowrange;
        string strCellData, strMsg;
        string strDataRow = "";
        long lngNumRows, lngNumCols = 0;
        long intModNumber, lngProgressPct = 0;
        double dblProgress = 0;
        double dblProgressPct = 0;
        string strOutputFileName = "";
        int intPos, intProgress = 0;
        string strSubFileName = "";
        string strFullOutputFileName = "";
        string strOutName = "";
        long lngColCnt;
        string[] strWSNames = new string[0];
        Type typCellType;
        bool blnMultiWS = false;
        string strNumberFormat;
        string strFileName = (string)e.Argument;
        //string strFileName = (string)((object[])e.Argument)[0];

        intPos = strFileName.IndexOf(".");
        strSubFileName = strFileName.Substring(0, intPos);
        strOutputFileName = strFileName.Substring(strFileName.LastIndexOf("\\") + 1);
        strOutName = strOutputFileName.Substring(0, strOutputFileName.IndexOf("."));

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open(strFileName, 0, true, 5, "", "", true,
            Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

        Excel.Sheets excelSheets = xlWorkBook.Worksheets;

        if (excelSheets.Count > 1)
        {
            blnMultiWS = CheckMultipleWorksheets(excelSheets);

            if (!blnMultiWS)
            {
                Array.Clear(strChoosenNames, 0, strChoosenNames.Length);
                Array.Resize(ref strChoosenNames, 1);
                strChoosenNames[0] = strOutName;
            }
        }
        else
        {
            Array.Resize(ref strChoosenNames, 1);
            strChoosenNames[0] = strOutName;
        }

        if (blnMWCancel)
        {
            strMsg = "Processing has been cancelled.";
            System.Windows.MessageBox.Show(strMsg);
            goto ReadWriteExcelDataExit;
        }

        foreach (string strCurrWSName in strChoosenNames)
        {
            //grdProgressIndicator.Visibility = Visibility.Visible;
            //txtIndicator.Text = "File: " + strCurrWSName;
            //prgIndicator.Width = 0;
            //lblPrctPrgrs.Content = "0%";
            //System.Windows.Forms.Application.DoEvents();

            //txtIndicator.Text = " Processing File: " + strCurrWSName + ".  Please wait...";
            strFullOutputFileName = strExOutputPath + "\\" + strCurrWSName + "_duc.txt";

            //if (strChoosenNames.Length > 1)
            //{
            //    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[strCurrWSName];
            //}
            //else
            //{
            //    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            //}

            try
            {
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[strCurrWSName];
            }
            catch (Exception exQuery)
            {
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            }


            if (!blnDoForAll)
            {
//  I ALSO TRIED TO USE A THREAD.  THIS SEEMS TO WORK EXCEPT THAT IT PROCESSING DOESN'T RUN IN THE FOREGROUND SO IT FALLS TO THE CODE AFTER BEFORE THE DIALOG IS DISPLAYED
                //Thread thread = new Thread(() =>
                //{
                //    ExcelRowColInfo ercWin = new ExcelRowColInfo(xlWorkSheet.Name);
                //    ercWin.Left = System.Windows.Application.Current.MainWindow.Left + 15;
                //    ercWin.Top = System.Windows.Application.Current.MainWindow.Top + 15;
                //    ercWin.ShowDialog();

                //    blnExParamCancel = ercWin.blnExCancel;
                //    strExcelStartCol = ercWin.strStartCol;
                //    strExcelEndCol = ercWin.strEndCol;
                //    lngExcelStartRow = ercWin.lngStartRow;
                //    lngExcelEndRow = ercWin.lngEndRow;
                //    blnLetExcelDecide = ercWin.blnExcelDecide;
                //    blnDoForAll = ercWin.blnDoForAll;
                //});
                //thread.SetApartmentState(ApartmentState.STA);
                //thread.IsBackground = false;
                //thread.Start();


//  THIS IS ONE ATTEMPT TO USE A DISPATCHER, STILL GET THE STA ERROR
                //ExcelRowColInfo ercWin = new ExcelRowColInfo(xlWorkSheet.Name);
                //ercWin.Left = System.Windows.Application.Current.MainWindow.Left + 15;
                //ercWin.Top = System.Windows.Application.Current.MainWindow.Top + 15;
                //ercWin.Dispatcher.BeginInvoke
                //    (
                //        System.Windows.Threading.DispatcherPriority.Normal,
                //        (Action)(() =>
                //            {
                //                ercWin.ShowDialog();
                //                blnExParamCancel = ercWin.blnExCancel;
                //            }
                //        )
                //    );
//这就是错误发生的地方

ExcelRowColInfo ercWin = new ExcelRowColInfo(xlWorkSheet.Name);
ercWin.Left = System.Windows.Application.Current.MainWindow.Left + 15;
ercWin.Top = System.Windows.Application.Current.MainWindow.Top + 15;
ercWin.ShowDialog();

blnExParamCancel = ercWin.blnExCancel;

if (blnExParamCancel)
{
    prgExcelProgress.Foreground = new SolidColorBrush(Colors.LightPink);
    Dispatcher.BeginInvoke((Action)delegate
    {
           prgExcelProgress.Value = 100;
           txtExcelPercent.Text = "Process has been cancelled";
    });

    blnExParamCancel = false;
    goto ReadWriteExcelDataExit;
}

strExcelStartCol = ercWin.strStartCol;
strExcelEndCol = ercWin.strEndCol;
lngExcelStartRow = ercWin.lngStartRow;
lngExcelEndRow = ercWin.lngEndRow;
blnLetExcelDecide = ercWin.blnExcelDecide;
blnDoForAll = ercWin.blnDoForAll;

if (blnLetExcelDecide)
{
      range = xlWorkSheet.UsedRange;
}
else
{
      Excel.Range c1 = xlWorkSheet.Cells[lngExcelStartRow, strExcelStartCol];
      Excel.Range c2 = xlWorkSheet.Cells[lngExcelEndRow, strExcelEndCol];
      range = (Excel.Range)xlWorkSheet.get_Range(c1, c2);
}

colrange = range.Columns;
lngNumCols = colrange.Count;
rowrange = range.Rows;
lngNumRows = rowrange.Count;

if (lngNumRows < 10)
{
      intModNumber = lngNumRows;
}
else
{
     intModNumber = lngNumRows / 10;
}

if (System.IO.File.Exists(@strFullOutputFileName))
{
      System.IO.File.Delete(@strFullOutputFileName);
}

object[,] values = (object[,])range.Value;
long NumRow = 1;

 using (StreamWriter file = new StreamWriter(@strFullOutputFileName, true, Encoding.GetEncoding("iso-8859-1")))
 {
        while (NumRow <= values.GetLength(0))
        {
              strDataRow = "";

              for (lngColCnt = 1; lngColCnt <= lngNumCols; lngColCnt++)
              {
                   if (values[NumRow, lngColCnt] == null)
                   {
                       typCellType = typeof(System.String);
                   }
                   else
                   {
                       typCellType = values[NumRow, lngColCnt].GetType();
                   }

                   strCellData = Convert.ToString(values[NumRow, lngColCnt]);
                   if (typCellType == typeof(System.DateTime))
                   {
                       strCellData = strCellData.Substring(0, strCellData.IndexOf(" "));
                   }
                        else
                        {
                            if (typCellType == typeof(System.Decimal))
                            {
                                if (Convert.ToDecimal(values[NumRow, lngColCnt]) == 0)
                                {
                                    strCellData = Convert.ToString(0);
                                }
                                else
                                {
                                    strCellData = Convert.ToString(values[NumRow, lngColCnt]);
                                }
                            }
                            else
                            {
                                if (typCellType != typeof(System.String))
                                {
                                    strNumberFormat = range[NumRow, lngColCnt].NumberFormat.ToString();

                                    if (strNumberFormat != "General" && strNumberFormat != "Text" && strNumberFormat != "@")
                                    {
                                        if (typCellType == typeof(System.Double))
                                        {
                                            if (strNumberFormat.IndexOf("[Red]") > 0)
                                            {
                                                strCellData = Convert.ToString(range[NumRow, lngColCnt].Value2);
                                            }
                                            else
                                            {
                                                double dblCellValue = double.Parse(strCellData);
                                                strCellData = dblCellValue.ToString(strNumberFormat);
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        if (strCellData == null)
                        {
                            strCellData = string.Empty;
                        }
                        else
                        {
                            strCellData = strCellData.Replace("\r\n", " ").Replace("\n", " ").Replace("\r", " ");
                        }

                        if (lngColCnt == lngNumCols)
                        {
                            strDataRow += strCellData;
                        }
                        else
                        {
                            strDataRow += strCellData + "|";
                        }
                    }

                    file.WriteLine(strDataRow);

                    if (NumRow % intModNumber == 0)
                    {
                        lngProgressPct = (NumRow / lngNumRows);

                        bgwExcelRunner.ReportProgress((int)lngProgressPct);
                    }

                    NumRow++;
                }
            }

            releaseObject(xlWorkSheet);
        }

        xlWorkBook.Close(false, null, null);

        ReadWriteExcelDataExit:
            blnMWCancel = false;
            xlApp.Quit();
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
    }
我试图将[StatThread]放在上面,但出现错误“Attributer'StatThread'在此声明类型上无效。它仅在'Method'声明上有效。”


如何正确执行此操作?

可能重复预测您的投诉,请参阅。总结:如果要在其线程中创建窗口,请不要使用
BackgroundWorker
。可以自己创建线程,也可以不在其中创建窗口(向STA线程发送窗口创建请求)。我已决定尝试将backgroundworker移动到只执行代码的一个子集,这样我就不必调用其中的任何对话框。但是,现在我在将Excel.Range传递到_dowork过程时遇到问题。该范围包含数据,并且在代码中运行良好,但当我尝试{bgwExcelRunner.RunWorkerAsync时(新对象[]{strFullOutputFileName,range})}range对象在_dowork过程中出错。如何传递range对象?
public ExcelRowColInfo(string strWSName)
    {
        InitializeComponent();
        strCurrWSName = string.Copy(strWSName);
        InitializeExcelParams();
    }