C# 设置后台工作人员的正确工作

C# 设置后台工作人员的正确工作,c#,C#,我在进度栏中设置BrackGrand worker的正确显示时遇到问题 也就是说,我希望加载到datagrid中,而ecel加载到我的进度条中。这是我当前的代码,它可以工作,但首先我得到excel,然后是backgroundworker,我如何设置它以并行方式完成 private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { int process = ((DataParame

我在进度栏中设置BrackGrand worker的正确显示时遇到问题

也就是说,我希望加载到datagrid中,而ecel加载到我的进度条中。这是我当前的代码,它可以工作,但首先我得到excel,然后是backgroundworker,我如何设置它以并行方式完成

      private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        int process = ((DataParameter)e.Argument).Process;
        int delay = ((DataParameter)e.Argument).Delay;
        int index = 1;

        try
        {
            //for (int i = 0; i < process; i++)
            //{
                //if (!backgroundWorker.CancellationPending)
                //{
                //    backgroundWorker.ReportProgress(index++ * 100 / process, string.Format("Process data {0}", i));
                //    Thread.Sleep(delay);
                //}
            // }

        }
        catch (Exception ex)
        {
            backgroundWorker.CancelAsync();
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progress.Value = e.ProgressPercentage;
        lblPercent.Text = string.Format("Processing...{0}%", e.ProgressPercentage);
        progress.Update();
    }

    private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Process has been completed.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    private void buttonBrowse_Click(object sender, EventArgs e)
    {
        OpenFileDialog fd = new OpenFileDialog();
        fd.Filter = "File Excel|*.xlsx";
        DialogResult re = fd.ShowDialog();
        excel_name = fd.SafeFileName;
        if (re == DialogResult.OK)
        {
            string fileName = fd.FileName;

            if (!backgroundWorker.IsBusy)
            {
                inputParameter.Delay = 100;
                inputParameter.Process = 200;
                backgroundWorker.RunWorkerAsync(inputParameter);
                ReadExcel(fileName); 
            }
        }
    }

    public void ReadExcel(string Path)
    {
        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
        Excel.Range range;

        int rCnt;
        int cCnt;
        int rw = 0;
        int cl = 0;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open(Path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        range = xlWorkSheet.UsedRange;
        rw = range.Rows.Count;
        cl = range.Columns.Count;
        int tenPercent = rw / 10;
        int percentCounter = 0;
        for (rCnt = 1; rCnt <= rw; rCnt++)
        {
            for (cCnt = 0; cCnt <= cl; cCnt++)
            {
                no = Convert.ToString((range.Cells[rCnt, 1] as Excel.Range).Value);
                name = Convert.ToString((range.Cells[rCnt, 2] as Excel.Range).Value);
                time_to = Convert.ToString((range.Cells[rCnt, 3] as Excel.Range).Value);
            }
            if (++percentCounter == tenPercent)
            {
                double progress = rCnt / (double)rw;
                lblPercent.Text = string.Format("Processing...{0}%", 100 * progress);
                percentCounter = 0;
            }
            DateTime dt = DateTime.Parse(no);
            string date =  dt.ToString("yyyy'/'MM'/'dd");

            double d = double.Parse(name);
            DateTime conv = DateTime.FromOADate(d);
            string time = conv.ToString("HH:mm:ss");

            double f = double.Parse(time_to);
            DateTime conv2 = DateTime.FromOADate(f);
            string time2 = conv2.ToString("HH:mm:ss");

            epg_id = ((range.Cells[rCnt, 4] as Excel.Range).Value);


            SQLiteConnection sqliteCon = new SQLiteConnection(dbConnectionString);
            sqliteCon.Open();
            string selectQuery = "SELECT * FROM EPGdaten WHERE File=@File";
            command = new SQLiteCommand(selectQuery, sqliteCon);
            command.Parameters.Add(new SQLiteParameter("@File", epg_id));
            sqdr = command.ExecuteReader();
            if (sqdr.Read())
            {
               string s = sqdr.GetValue(1).ToString();
               string b = sqdr.GetValue(3).ToString();
               dataGridView1.Rows.Add(date, time, time2, s, b);
            }
            else
            {
                dataGridView1.Rows.Add(date, time, time2);
            }


            sqdr.Close();
            sqliteCon.Close();
            sqliteCon.Dispose();
        }
        xlWorkBook.Close(true, null, null);
        xlApp.Quit();

        Marshal.ReleaseComObject(xlWorkSheet);
        Marshal.ReleaseComObject(xlWorkBook);
        Marshal.ReleaseComObject(xlApp);
    }
private void backgroundWorker\u DoWork(对象发送方,DoWorkEventArgs e)
{
int进程=((数据参数)e.Argument).process;
int delay=((数据参数)e.Argument).delay;
int指数=1;
尝试
{
//for(int i=0;i对于(rCnt=1;rCnt将这样的代码放入ReadExcel方法中

            int rw = 967;
            int tenPercent = rw / 10;
            int percentCounter = 0;
            for (int rCnt = 1; rCnt <= rw; rCnt++)
            {

                if( ++percentCounter == tenPercent )
                {
                    double progress = rCnt / (double)rw;
                    Console.WriteLine("Progress : {0}%", 100 * progress);
                    percentCounter = 0;
                }
            }
            Console.ReadLine();
int rw=967;
int百分之十=rw/10;
int percentCounter=0;

对于(int rCnt=1;rCnt)对ReadExcel()的调用不应该在backgroundWorker_DoWork()中移动吗方法?我认为您当前的版本在启动后台工作程序之前完全读取excel。该后台工作程序当前没有做任何实际工作,是吗?在读取excel之前启动后台工作程序。后台工作程序正在异步运行,并将在您读取excel文件时运行。是的。ReadExcel是同步的method和backgroundworker在ReadExcel完成之前不会启动。在backgroundworker启动后移动ReadExcel()。@jdweng您能根据我的代码告诉我应该将其放在哪里吗?我做了错误的事情,只是显示了我。感谢方法按钮中的一个提示Unloadexcel\u Click()移动ReadExcel()到最后一行。在for循环结束时,您应该再进行一次更新,并将其设置为100%。分数很少精确到10%的步长。