1

大きなファイルをロードするために BackgroundWorker を使用しています。選択したファイルに応じて、4000 行または 400,000 行のみを持つことができます。列の数も異なります。

行をバッチで段階的に、しかし迅速に追加したいと思います。バッチを構成する行数を変更しました。私の問題は、行の最初のバッチが FlexGrid 罰金に追加されることです。追加する 2 番目のバッチが必要なほどファイルが大きい場合、グリッドのスクロールバーが揺れて画面がフリーズします。

ボタン クリック メソッド内で RunWorderAsync メソッドを呼び出して、ファイルを選択します。また、FlexGrid をデータ ソースとしてデータ テーブルに割り当てます。

コードは次のとおりです。

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

                // go load the data
                backgroundWorker1.RunWorkerAsync(lengthOfFile);

FlexGrid は ComponentOne からのもので、WinForms に使用されます。

DoWorker コードは次のとおりです。

// 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 

}

ProgressChanged メソッドは次のとおりです。

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

0 に答える 0