BackgroundWorker和ReportProgress冻结UI

BackgroundWorker和ReportProgress冻结UI,backgroundworker,Backgroundworker,我正在使用BackgroundWorker加载大文件。根据选择的文件,它只能有4000行或400000行。列的数量也各不相同 我想逐步但快速地批量添加行。我改变了组成一批的行数。我的问题是第一批行被添加到FlexGrid中。如果文件足够大,需要添加第二批,则网格的滚动条会自动移动,屏幕会冻结 我在按钮单击方法中调用RunWorderAsync方法来选择文件。我还将FlexGrid分配给数据表作为其数据源 代码如下: //bind the grid to the e

我正在使用BackgroundWorker加载大文件。根据选择的文件,它只能有4000行或400000行。列的数量也各不相同

我想逐步但快速地批量添加行。我改变了组成一批的行数。我的问题是第一批行被添加到FlexGrid中。如果文件足够大,需要添加第二批,则网格的滚动条会自动移动,屏幕会冻结

我在按钮单击方法中调用RunWorderAsync方法来选择文件。我还将FlexGrid分配给数据表作为其数据源

代码如下:

                //bind the grid to the empty table
                c1FlexGrid1.DataSource = dt;

                // go load the data
                backgroundWorker1.RunWorkerAsync(lengthOfFile);
// create rows for data table
    using (StreamReader streamRead = new StreamReader(filePathAndName))
        while ((lineOfData = streamRead.ReadLine()) != null)
        {
            rowCount++;
            sizeOfLine += lineOfData.Length;
            //create a new row in the data table
            var row = dt.NewRow();
            arrayOfData = lineOfData.Split(',');

            for (int j = 0; j < dt.Columns.Count; j++)
            {
                row[j] = arrayOfData[j];
            }
            newRowData.Add(row);

            percentComplete += 10;
            // inform progress at every 10 percent
            currentPercentComplete = ((double)sizeOfLine / sizeOfFile) * 100;
            if(((int)currentPercentComplete > percentComplete) && (percentComplete < 100))
            {
                Console.WriteLine("Starting UI thread - row count {0}, percent complete {1}, current percent {2}", rowCount, percentComplete, currentPercentComplete);
                percentComplete += 10;
                backgroundWorker1.ReportProgress(percentComplete);

                // simulate lengthy operation
                System.Threading.Thread.Sleep(1000);
            }

        }//end while loop

    }//end of using 

}
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {

        dt.BeginLoadData();
        for (int i = 0; i < newRowData.Count; i++)
        {
            dt.Rows.Add(newRowData[i]);
        }
        dt.EndLoadData();

//        Console.WriteLine("In UI thread ProgressChanged - end load -rows added - {0}", newRowData.Count);

        // done with this batch
        newRowData.Clear();

        // update UI
        this.progressBar1.Value = e.ProgressPercentage;
        Application.DoEvents();
    }
FlexGrid来自ComponentOne,用于WinForms

DoWorker代码如下所示:

                //bind the grid to the empty table
                c1FlexGrid1.DataSource = dt;

                // go load the data
                backgroundWorker1.RunWorkerAsync(lengthOfFile);
// create rows for data table
    using (StreamReader streamRead = new StreamReader(filePathAndName))
        while ((lineOfData = streamRead.ReadLine()) != null)
        {
            rowCount++;
            sizeOfLine += lineOfData.Length;
            //create a new row in the data table
            var row = dt.NewRow();
            arrayOfData = lineOfData.Split(',');

            for (int j = 0; j < dt.Columns.Count; j++)
            {
                row[j] = arrayOfData[j];
            }
            newRowData.Add(row);

            percentComplete += 10;
            // inform progress at every 10 percent
            currentPercentComplete = ((double)sizeOfLine / sizeOfFile) * 100;
            if(((int)currentPercentComplete > percentComplete) && (percentComplete < 100))
            {
                Console.WriteLine("Starting UI thread - row count {0}, percent complete {1}, current percent {2}", rowCount, percentComplete, currentPercentComplete);
                percentComplete += 10;
                backgroundWorker1.ReportProgress(percentComplete);

                // simulate lengthy operation
                System.Threading.Thread.Sleep(1000);
            }

        }//end while loop

    }//end of using 

}
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {

        dt.BeginLoadData();
        for (int i = 0; i < newRowData.Count; i++)
        {
            dt.Rows.Add(newRowData[i]);
        }
        dt.EndLoadData();

//        Console.WriteLine("In UI thread ProgressChanged - end load -rows added - {0}", newRowData.Count);

        // done with this batch
        newRowData.Clear();

        // update UI
        this.progressBar1.Value = e.ProgressPercentage;
        Application.DoEvents();
    }
ProgressChanged方法如下所示:

                //bind the grid to the empty table
                c1FlexGrid1.DataSource = dt;

                // go load the data
                backgroundWorker1.RunWorkerAsync(lengthOfFile);
// create rows for data table
    using (StreamReader streamRead = new StreamReader(filePathAndName))
        while ((lineOfData = streamRead.ReadLine()) != null)
        {
            rowCount++;
            sizeOfLine += lineOfData.Length;
            //create a new row in the data table
            var row = dt.NewRow();
            arrayOfData = lineOfData.Split(',');

            for (int j = 0; j < dt.Columns.Count; j++)
            {
                row[j] = arrayOfData[j];
            }
            newRowData.Add(row);

            percentComplete += 10;
            // inform progress at every 10 percent
            currentPercentComplete = ((double)sizeOfLine / sizeOfFile) * 100;
            if(((int)currentPercentComplete > percentComplete) && (percentComplete < 100))
            {
                Console.WriteLine("Starting UI thread - row count {0}, percent complete {1}, current percent {2}", rowCount, percentComplete, currentPercentComplete);
                percentComplete += 10;
                backgroundWorker1.ReportProgress(percentComplete);

                // simulate lengthy operation
                System.Threading.Thread.Sleep(1000);
            }

        }//end while loop

    }//end of using 

}
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {

        dt.BeginLoadData();
        for (int i = 0; i < newRowData.Count; i++)
        {
            dt.Rows.Add(newRowData[i]);
        }
        dt.EndLoadData();

//        Console.WriteLine("In UI thread ProgressChanged - end load -rows added - {0}", newRowData.Count);

        // done with this batch
        newRowData.Clear();

        // update UI
        this.progressBar1.Value = e.ProgressPercentage;
        Application.DoEvents();
    }

我没有看到任何更新UI的代码,这意味着我在猜测是什么让你的UI变得迟钝。