N2J
N2J

Reputation: 175

Reading txt files using task concept working slow

my requirement was reading some 300 text files each of around 40-70 MB line by line to perform some kind of checks . As the files are huge so I thought of using TPL concept i.e. tasks. if I am not using task concept, it is taking around 7 minutes but if I am doing it by using task , it is taking long time don't know why. I will be so thankful if someone please review my code and tell me where I am doing wrong in using task. Any help would be appreciated. Below is my code :

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

Upvotes: 1

Views: 70

Answers (1)

Britto Raj
Britto Raj

Reputation: 451

Since you are using Task.WaitAll(tasks.ToArray()). Wait will synchronously block until the task completes. But can you try with await (Not in TPL), await will asynchronously wait until the task completes

Upvotes: 1

Related Questions