-1

This code is for summary report for peak hours. it froze my application for 10 seconds and get back. how to stop frozing

 else if (reportType == "Peak Hour")
                {
                    // VARIABLES
                    DateTime time = Convert.ToDateTime("12:00 AM");
                    string counterTime;
                  //  DateTime sextime = Convert.ToDateTime(time.ToString("hh:00 tt"));
                    // 1st LOOPING
                    while (true)
                    {
                        counterTime = time.ToString("hh:00 tt");
                        time = time + TimeSpan.FromMinutes(1);
                        if ("12:00 AM" == time.ToString("hh:mm tt"))
                        {
                            break;
                        }

                        foreach (DateTime day in EachDay(dateTimePickerFrom.Value, dateTimePickerTo.Value))
                        {

                            foreach (DataGridViewRow row in dataGridViewTrip.Rows)
                            {
                                // CONVERT STRING TO DATE 
                                if (row.Cells[0].Value.ToString() == day.ToString("yyyy-MM-dd"))
                                {
                                    if (row.Cells[1].Value.ToString() == time.ToString("hh:mm tt"))
                                    {
                                        totalpassengerDaily = int.Parse(row.Cells[4].Value.ToString()) + totalpassengerDaily;
                                        totalCommission = Decimal.Parse(row.Cells[8].Value.ToString()) + totalCommission;
                                    }
                                }
                            }
                        }
                        if (counterTime != time.ToString("hh:00 tt"))
                        {
                            if (totalpassengerDaily > 0 || totalCommission > 0)
                            {

                                // INSERT TO CHART
                                chartDaily.Series["Passengers"].Points.AddXY(counterTime, totalpassengerDaily);
                                chartDaily.Series["Commission"].Points.AddXY(counterTime, totalCommission);
                                // ADDING TO DATAGRID
                                DataRow row = summaryReport.NewRow();
                                row["Date"] = counterTime;
                                row["Passenger"] = totalpassengerDaily;
                                row["Commission"] = totalCommission;
                                summaryReport.Rows.Add(row);
                                // RESET 
                                totalpassengerDaily = 0;
                                totalCommission = 0;
                            }
                        }
                        // dito
                    }
                    totalpassengerDaily = 0;
                    totalCommission = 0;
                    froms = dateTimePickerFrom.Value.ToString("dd MMMM yyyy");
                    tos = dateTimePickerTo.Value.ToString("dd MMMM yyyy");
                    //   reportType = "Peak Hour \n From:" + dateTimePickerFrom.Value.ToString("dd-MMMM-yyyy") + " To:" + dateTimePickerTo.Value.ToString("dd-MMMM-yyyy");
                }

it also effect some controls from the form like the all the border in buttons.

This course aims to teach everyone the basics of programming computers using Python. We cover the basics of how one constructs a program from a series of simple instructions in Python. The course has no pre-requisites and avoids all but the simplest mathematics. Anyone with moderate computer experience should be able to master the materials in this course. This course will cover Chapters 1-5 of the textbook “Python for Everybody”. Once a student completes this course, they will be ready to take more advanced programming courses. This course covers Python 3.

1 Answers1

0

You are playing with the file when use while(true) :). There is a const number of minutes in a day, so just use for loop: for(i=0; i<60*24; i++) {...}

You can use performance profiler to identify hot path in your code. However, from your code I see that you are iterating 1440 times for each date over all rows in your grid, what is very expensive. I'd recommend to change algorithm to select rows in 1 iteration and then accumulate them by a minute.

var days = new HashSet(EachDay(dateTimePickerFrom.Value, dateTimePickerTo.Value).Select(day=>day.ToString("yyyy-MM-dd")))
var dates = dataGridViewTrip.Rows
               .Where(row => days.Contains(row.Cells[0].Value)) //select only dates from picker
               .GroupBy(r=>DateTime.Parse(row.Cells[0].Value)); // group by date
foreach(var date in dates ) // iterate over dates to calculate p
{
  var totalpassengerDaily = 0;
  var totalCommission = 0;
  var times = date.GroupBy(r=>DateTime.Parse(r.Cells[1].Value).TimeOfDay) // group by time
                  .OrderBy(r=>g.Key) // sort by time
                  .Select(g=> new { Time = g.Key,
                            TotalpassengerDaily=(totalpassengerDaily +=g.Sum(row=>int.Parse(row.Cells[4].Value.ToString()))),
                            TotalCommission =(totalCommission +=g.Sum(row=>Decimal.Parse(row.Cells[8].Value.ToString())))});

// it takes only existing times, if you need to fill gaps 
// just use for loop to iterate over all minutes
  foreach(time in times) {
    chartDaily.Series["Passengers"].Points.AddXY(time.Time, time.TotalpassengerDaily);
    chartDaily.Series["Commission"].Points.AddXY(time.Time, time.TotalCommission );

    DataRow row = summaryReport.NewRow();
    row["Date"] = time.Time;
    row["Passenger"] = time.TotalpassengerDaily;
    row["Commission"] = time.TotalCommission;
    summaryReport.Rows.Add(row);
  }

}
fenixil
  • 2,106
  • 7
  • 13
  • the code doesnt work for me. but thank you about the performance profiler – Ernesto Arcillas Sep 15 '19 at 20:48
  • Please let me know what does not work, I tried it on the stub data, it might not respect types in data grid. Idea behind this code is that it is more efficient, profiler will show you hot paths, but still you'll need to change an algorytm to make it more efficient. – fenixil Sep 15 '19 at 20:53
  • I am sorry but i am not familiar with advance c#. i'm just new and, there are many errors which i can't fix. i fixed the hashset by including the library but, the ".Where" , "row", "time"; – Ernesto Arcillas Sep 16 '19 at 00:26