使用任务概念读取 txt 文件工作缓慢

本文关键字:txt 文件 工作 缓慢 读取 任务 | 更新日期: 2023-09-27 18:33:05

我的要求是逐行读取大约 300 个文本文件,每个文本文件大约 40-70 MB 以执行某种检查。由于文件很大,所以我想使用TPL概念,即任务。如果我不使用任务概念,大约需要 7 分钟,但如果我使用任务来做到这一点,则需要很长时间不知道为什么。如果有人请查看我的代码并告诉我在使用任务时做错了哪里,我将不胜感激。任何帮助将不胜感激。以下是我的代码:

private void browse_Click(object sender, EventArgs e)
{
    try
    {
        string newFileName1 = "";
        string newFileName2 = "";
        week = textBox2.Text;
        if (week == null || week == "")
        {
            MessageBox.Show("Week cannot be null.");
            return;
        }
        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            DateTime starttime = DateTime.Now;
            string folderPath = Path.GetDirectoryName(folderBrowserDialog1.SelectedPath);
            string folderName = Path.GetFileName(folderPath);
            DirectoryInfo dInfo = new DirectoryInfo(folderPath);
            foreach (DirectoryInfo folder in dInfo.GetDirectories())
            {
                newFileName1 = "Files_with_dates_mismatching_the_respective_week_" + folder.Name + ".txt";
                newFileName2 = "Files_with_wrong_date_format_" + folder.Name + ".txt";
                if (File.Exists(folderPath + "/" + newFileName1))
                {
                    File.Delete(folderPath + "/" + newFileName1);
                }
                if (File.Exists(folderPath + "/" + newFileName2))
                {
                    File.Delete(folderPath + "/" + newFileName2);
                }
                FileInfo[] folderFiles = folder.GetFiles();
                if (folderFiles.Length != 0)
                {
                    List<Task> tasks = new List<Task>();
                    foreach (var file in folderFiles)
                    {
                        var task = Task.Factory.StartNew(() =>
                                                    {
                                                        bool taskResult = ReadFile(file.FullName, folderPath, folderName, week);
                                                        return taskResult;
                                                    });
                        tasks.Add(task);
                    }
                    Task.WaitAll(tasks.ToArray());
                    DateTime stoptime = DateTime.Now;
                    TimeSpan totaltime = stoptime.Subtract(starttime);
                    label6.Text = Convert.ToString(totaltime);
                    textBox1.Text = folderPath;
                    DialogResult result2 = MessageBox.Show("Read the files successfully.", "Important message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
    catch (Exception)
    {
        throw;
    }
}
public bool ReadFile(string file, string folderPath, string folderName, string week)
{
    int LineCount = 0;
    string fileName = Path.GetFileNameWithoutExtension(file);
    using (FileStream fs = File.Open(file, FileMode.Open))
    using (BufferedStream bs = new BufferedStream(fs))
    using (StreamReader sr = new StreamReader(bs))
    {
        for (int i = 0; i < 2; i++)
        {
            sr.ReadLine();
        }
        string oline;
        while ((oline = sr.ReadLine()) != null)
        {
            LineCount = ++LineCount;
            string[] eachLine = oline.Split(';');
            string date = eachLine[30].Substring(1).Substring(0, 10);
            DateTime dt;
            bool valid = DateTime.TryParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
            if (!valid)
            {
                Filecount = ++Filecount;
                StreamWriter sw = new StreamWriter(folderPath + "/" + "Files_with_wrong_date_format_" + folderName + ".txt", true);
                sw.WriteLine(fileName + "  " + "--" + "  " + "Line number :" + " " + LineCount);
                sw.Close();
            }
            else
            {
                DateTime Date = DateTime.ParseExact(date, "d/M/yyyy", CultureInfo.InvariantCulture);
                int calculatedWeek = new GregorianCalendar(GregorianCalendarTypes.Localized).GetWeekOfYear(Date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Saturday);
                if (calculatedWeek == Convert.ToInt32(week))
                {
                }
                else
                {
                    Filecount = ++Filecount;
                    StreamWriter sw = new StreamWriter(folderPath + "/" + "Files_with_dates_mismatching_the_respective_week_" + folderName + ".txt", true);
                    sw.WriteLine(fileName + "  " + "--" + "  " + "Line number :" + " " + LineCount);
                    sw.Close();
                }
            }
        }
    }
    return true;
}

使用任务概念读取 txt 文件工作缓慢

由于您使用的是 Task.WaitAll(tasks.ToArray())。等待将同步阻止,直到任务完成。但是你能尝试使用await(不在TPL中)吗,await将异步等待任务完成