0

複数の .txt ファイルからデータを検索するツールを作成しようとしています。ユーザーが作業の進行状況を追跡するための進行状況バーを作成したいと考えています。これが私が得たものです

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    String terms;
    StringBuilder sb;
    if (filePath != "")
    {
        sb = new StringBuilder();
        //I use another function to add path to string filePath, 
        //seperated by \r\n
        String[] fpath = filePath.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string path in fpath)
        {
            if (!String.IsNullOrEmpty(path))
            {                     
                lines = File.ReadAllLines(path);
                for (int i = 0; i < lines.Count(); i++)
                {
                    terms = lines[i];
                    if (terms.Contains("Content to look for"))
                    {
                        sb.AppendLine(lines[i]);                                
                    }
                    //Problem here
                    backgroundWorker1.ReportProgress(i);
                }
            }
        }
    }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value += (progressBar1.Maximum * e.ProgressPercentage / 100);
    lblPercent.Text = e.ProgressPercentage.ToString() + " %";
}

行を追加すると:

backgroundWorker1.ReportProgress(i);

例外がスローされます:

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

Ivoke を使用しようとしましたが、それでもこの例外がスローされます。

Invoke((Action)(() =>
{
    String terms;
    StringBuilder sb;
    if (filePath != "")
    {
        sb = new StringBuilder();
        String[] fpath = filePath.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string path in fpath)
        {
            if (!String.IsNullOrEmpty(path))
            {
                lines = File.ReadAllLines(path);
                for (int i = 0; i < lines.Count(); i++)
                {
                    terms = lines[i];
                    if (terms.Contains("Content to look for"))
                    {
                        sb.AppendLine(lines[i]);
                    }
                    backgroundWorker1.ReportProgress(i);
                }
            }
        }
    }
}));

どこが間違っているか教えていただけますか?

4

0 に答える 0