使用任务 (TPL) 读取大量文本文件

本文关键字:文本 文件 读取 任务 TPL | 更新日期: 2023-09-27 18:32:59

我正在使用任务概念读取大约 300 个 txt 文件。我的要求是读取每个文件,从每一行中选择日期,检查它来的一周,然后将其与文件夹名称进行比较,文件夹名称实际上是周名(如 41、42),无论它们是否相同。 如果没有,则写下文件名和行号。由于文件的数量和文件的大小是巨大的,我正在尝试使用任务概念来加快过程。下面是我的代码。

任何帮助将不胜感激。提前谢谢。

    private void browse_Click(object sender, EventArgs e)
    {
        try
        {
            string newFileName = "";
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                DateTime starttime = DateTime.Now;
                string folderPath = Path.GetDirectoryName(folderBrowserDialog1.SelectedPath);
                DirectoryInfo dInfo = new DirectoryInfo(folderPath);
                string[] Allfiles = Directory.EnumerateFiles(folderPath, "*.txt", SearchOption.AllDirectories).ToArray();
                foreach (DirectoryInfo folder in dInfo.GetDirectories())
                {
                    newFileName = "Files_with_duplicate_TGMKT_Names_in_child_Folder_" + folder.Name + ".txt";
                    if (File.Exists(folderPath + "/" + newFileName))
                    {
                        File.Delete(folderPath + "/" + newFileName);
                    }
                    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,newFileName);
                                                            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 newFileName)
    {
        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 Date = DateTime.ParseExact(date, "d", CultureInfo.InvariantCulture);
                int week = new GregorianCalendar(GregorianCalendarTypes.Localized).GetWeekOfYear(Date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Saturday);
                if (Convert.ToString(week) == folderName)
                {
                }
                else
                {
                    using (StreamWriter sw = new StreamWriter(folderPath + "/" + newFileName, true))
                    {
                        Filecount = ++Filecount;
                        sw.WriteLine(fileName + "  " + "--" + "  " + "Line number :" + " " + LineCount);
                    }
                }
            }
        }
        return true;

    }

使用任务 (TPL) 读取大量文本文件

您的调用MessageBox.Show ReadFile 内部,这意味着每个文件都必须写入一条消息。这样,您将获得300条消息。

尝试将消息放在ReadFile方法之外的WaitAll调用之后。

                if (files.Length != 0)
                {
                    List<Task> tasks = new List<Task>();
                    foreach (var file in files)
                    {
                        var task = Task.Factory.StartNew(() =>
                                                    {
                                                        bool taskResult = ReadFile(file);
                                                        return taskResult;
                                                    });
                        tasks.Add(task);
                    }
                    Task.WaitAll(tasks.ToArray());
                    // Message here
                    DialogResult result2 = MessageBox.Show("Read the files successfully.", "Important message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }