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

时间:2016-02-02 05:48:54

标签: c#

我正在使用任务概念阅读大约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;


    }

1 个答案:

答案 0 :(得分:1)

MessageBox.Show内的ReadFile,这意味着每个文件都必须写一条消息。通过这种方式,您将获得300条消息。

尝试在WaitAll方法之外的ReadFile电话之后发送消息。

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